SOLID Presentation
SOLID Presentation
COHESION:
COUPLING:
“The degree to which each program module relies on each one of the
other modules” – Wikipedia
ENCAPSULATION:
“The hiding of design decisions in a computer program that are most likely
to change” - Wikipedia
S.O.L.I.D. Principles
There should never be more than one reason for a class to change.
Robert C. “Uncle Bob” Martin
Cohesion and Coupling
• “a reason to change”
Example App New Requirements: Send From Non-WinForms App. Read XML Or Flat File
Example App: A Better Structure
Summary
• Related Fundamentals:
oOpen/Closed Principle
oInterface Segregation Principle
oSeparation of Concerns
• Recommended Reading:
oClean Code by Robert C. Martin [http://amzn.to/Clean-Code]
The Open / Closed Principle
OCP: The Open/Closed Principle
Wikipedia
The Open / Closed Principle
Open to Extension
New behavior can be added in the future
Closed to Modification
Changes to source or binary code are not required
Dr. Bertrand Meyer originated the OCP term in his 1988 book,
Object Oriented Software Construction
Change behavior without changing code?
Rely on abstractions
No limit to variety of implementations of each
abstraction
Abstractions include:
• Interfaces
• Abstract Base Classes
Remember TANSTAAFL
• There Ain’t No Such Thing As A Free Lunch
• OCP adds complexity to design
• No design can be closed against all changes
Summary
• Related Fundamentals:
oSingle Responsibility Principle
oStrategy Pattern
oTemplate Method Pattern
• Recommended Reading:
oAgile Principles, Patterns, and Practices by Robert
C. Martin and Micah Martin
[http://amzn.to/agilepppcsharp]
The Liskov Substitution Principle
LSP: The Liskov Substitution Principle
Named for Barbara Liskov, who first described the principle in 1988.
Substitutability
And in general must not require calling code to know they are
different from their base type.
Inheritance and the IS-A Relationship
• Design By Contract is a technique that makes defining these pre- and post-
conditions explicit within code itself.
• To follow LSP, derived classes must not violate any constraints defined (or
assumed by clients) on the base classes
When do we fix LSP?
Corollary:
Prefer small, cohesive interfaces to “fat” interfaces
ISP: Interface
Segregation Principle
“This principle deals with the disadvantages of ‘fat’ interfaces. Classes
that have ‘fat’ interfaces are classes whose interfaces are not cohesive.
In other words, the interfaces of the class can be broken up into groups
of member functions. Each group serves a different set of clients. Thus
some clients use one group of member functions, and other clients use
the other groups.”
- Robert Martin
ISP: Interface
Segregation Principle
Example App: Clarifying The Email Sender and Message Info Parsing
The Dependency Inversion Principle
DIP: Dependency Inversion
Principle
“What is it that makes a design rigid, fragile and immobile? It is the interdependence of the modules within that design. A design is rigid if it cannot be easily changed. Such rigidity is due to the fact that a single change to heavily interdependent software begins a cascade of changes in dependent modules.”
- Robert Martin
DIP: The Dependency Inversion Principle
• Framework
• Third Party Libraries
• Database
• File System
• Email
• Web Services
• System Resources (Clock)
• Configuration
• The new Keyword
• Static methods
• Thread.Sleep
• Random
Dependency Injection
• Related Fundamentals:
o Single Responsibility Principle
o Interface Segregation Principle
o Façade Pattern
o Inversion of Control Containers
• Recommended Reading:
o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin
[http://amzn.to/agilepppcsharp]
o http://www.martinfowler.com/articles/injection.html
The Problem
• Result
o Tight coupling
o No way to change implementation details without recompile (OCP violation)
o Difficult to test
Dependency Injection
• Dependency is transitive
o If UI depends on BLL depends on DAL depends on Database Then
*everything* depends on the Database
• Related Fundamentals:
o Open Closed Principle
o Interface Segregation Principle
o Strategy Pattern
• Recommended Reading:
o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin
[http://amzn.to/agilepppcsharp]
o http://www.martinfowler.com/articles/injection.html
Don’t Repeat Yourself
Variations include:
• Once and Only Once
• Duplication Is Evil (DIE)