SOLID Design principles

sanaulla123 3,515 views 50 slides Nov 01, 2011
Slide 1
Slide 1 of 50
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50

About This Presentation

No description available for this slideshow.


Slide Content

'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()
Computation
application
GUI
application
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()
Computation
application
GUI
application
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
Tags