View
4.249
Download
2
Category
Preview:
DESCRIPTION
Citation preview
'SOLID' Desgin Principles
Our aim is to make software more stable unlike ...
We write code that is …
Rigid
Fragile
Immobile
Making everyone's life miserable
Source: http://www.welaf.com/13500,miserable-life-of-a-small-cat.html
So WHY SOLID?
It helps us to write code that is ...
Loosely coupled
Highly cohesive
Easily composable
Reusable
SOLID
Coined by Robert C Martin
Not new, exsiting principles brought together
Single Responsibility Principle (SRP)
Single Responsibility Principle (SRP)
Open Closed Principle (OCP)
Single Responsibility Principle (SRP)
Open Closed Principle (OCP)
Liskov Substitution Principle (LSP)
Single Responsibility Principle (SRP)
Open Closed Principle (OCP)
Liskov Substitution Principle (LSP)
Interface Seggregation Principle (ISP)
Single Responsibility Principle (SRP)
Open Closed Principle (OCP)
Liskov Substitution Principle (LSP)
Interface Seggregation Principle (ISP)
Dependency Inversion Principle (DIP)
Single Responsibility Principle
Single Responsibility Principle
”There should be NEVER be more than ONE reason for a class to change”
Single Responsibility Principle
Rectangle-----------------------
Draw()Area()
Computationapplication
GUIapplication
GUI
What is the issue with this design?
Single Responsibility Principle
Rectangle does Computes the area Draws it on the GUI
How can we fix it?
Single Responsibility Principle
Rectangle-----------------------
Area()
Computationapplication
GUIapplication
GUI
GUIRectangle-----------------------
Draw()
INHERITS
Single Responsibility Principle
JDK follows this (java.awt package) Graphics2D – drawing shapes Shape – for representing the geometrical shapes
Some other examples: Task to download a file, parse it, and store it in a
database UserSetting- provide customisation feature, check
Access
Open Closed Principle
Open Closed Principle
”Modules must be OPEN for extension, CLOSED for modification”
Open Closed Principle
Coined by Bertrand Meyer
Open Closed Principle
How can you add new features without editing the existing module?
Open Closed Principle
Extend the existing modules!
Open Closed Principle
How is it even possible?
Open Closed Principle
Depend on abstractions, new features can be added by extending the abstractions.
Open Closed Principle
Examples-- Drawing shapes
- Loan approval process
Liskov Substitution Principle
Liskov Subsitution Principle
”Base classes instances must be replaceable by the sub class instances without any change in the
application”
Liskov Subsitution Principle
Lets go back to an earlier example of Drawing shape
Liskov Subsitution Principle
Other examples- - Bird, Flight and Non-Flight
- Loading of settings-Rectangle and Square
Interface Seggregation Principle
Inteface Seggregation Principle
”Clients should not depend upon the interfaces they do not use”
Inteface Seggregation Principle
How can we pollute the interfaces? OR
How do we end up creating Fat interfaces?
Inteface Seggregation Principle
Door, Time and TimedDoor example. This example also violates LSP
Inteface Seggregation Principle
Possible Solutions Use delegation- Adapter Pattern Use multiple inheirtance- Interfaces, Mixins
Dependency Inversion Principle
Dependency Inversion Principle
”High level modules should not depend on the low level details modules, instead both should depend
on abstractions”
Dependency Inversion Principle
Copy
Read Keyboard Write Printer
What's GOOD or BAD about this design?
Dependency Inversion Principle
Copy program is NOT REUSABLE Tightly bound to Keyboard and Printer
Read Keyboard and Write Printer are REUSABLE
Dependency Inversion Principle
Copy program shouldn't be dependent on the Low level Read/Write modules.
How can we correct this?
Dependency Inversion Principle
Copy
Keyboard Reader Print Writer
Reader Writer
Copy program now depends on Abstractions- Reader and Writer
Resources
Wikipedia – SOLID OO Design Robert C Martin articles. Jim Weirich SOLID Ruby talk at Ruby
Conference 2009. Rob Martin interview at Hansel Minutes. … and various other small articles,
presentations
Ideas for next learning sessions?
GoF Design Patterns One or two patterns each week with code samples
JVM Internals NoSQL Java and Concurrency Functional programming in Java
Recommended