SOLID Design principles

50
'SOLID' Desgin Principles

description

 

Transcript of SOLID Design principles

Page 1: SOLID Design principles

'SOLID' Desgin Principles

Page 2: SOLID Design principles

Our aim is to make software more stable unlike ...

Page 3: SOLID Design principles

We write code that is …

Page 4: SOLID Design principles

Rigid

Page 5: SOLID Design principles

Fragile

Page 6: SOLID Design principles

Immobile

Page 7: SOLID Design principles

Making everyone's life miserable

Source: http://www.welaf.com/13500,miserable-life-of-a-small-cat.html

Page 8: SOLID Design principles

So WHY SOLID?

Page 9: SOLID Design principles

It helps us to write code that is ...

Page 10: SOLID Design principles

Loosely coupled

Page 11: SOLID Design principles

Highly cohesive

Page 12: SOLID Design principles

Easily composable

Page 13: SOLID Design principles

Reusable

Page 14: SOLID Design principles

SOLID

Coined by Robert C Martin

Not new, exsiting principles brought together

Page 15: SOLID Design principles

Single Responsibility Principle (SRP)

Page 16: SOLID Design principles

Single Responsibility Principle (SRP)

Open Closed Principle (OCP)

Page 17: SOLID Design principles

Single Responsibility Principle (SRP)

Open Closed Principle (OCP)

Liskov Substitution Principle (LSP)

Page 18: SOLID Design principles

Single Responsibility Principle (SRP)

Open Closed Principle (OCP)

Liskov Substitution Principle (LSP)

Interface Seggregation Principle (ISP)

Page 19: SOLID Design principles

Single Responsibility Principle (SRP)

Open Closed Principle (OCP)

Liskov Substitution Principle (LSP)

Interface Seggregation Principle (ISP)

Dependency Inversion Principle (DIP)

Page 20: SOLID Design principles

Single Responsibility Principle

Page 21: SOLID Design principles

Single Responsibility Principle

”There should be NEVER be more than ONE reason for a class to change”

Page 22: SOLID Design principles

Single Responsibility Principle

Rectangle-----------------------

Draw()Area()

Computationapplication

GUIapplication

GUI

What is the issue with this design?

Page 23: SOLID Design principles

Single Responsibility Principle

Rectangle does Computes the area Draws it on the GUI

How can we fix it?

Page 24: SOLID Design principles

Single Responsibility Principle

Rectangle-----------------------

Area()

Computationapplication

GUIapplication

GUI

GUIRectangle-----------------------

Draw()

INHERITS

Page 25: SOLID Design principles

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

Page 26: SOLID Design principles

Open Closed Principle

Page 27: SOLID Design principles

Open Closed Principle

”Modules must be OPEN for extension, CLOSED for modification”

Page 28: SOLID Design principles

Open Closed Principle

Coined by Bertrand Meyer

Page 29: SOLID Design principles

Open Closed Principle

How can you add new features without editing the existing module?

Page 30: SOLID Design principles

Open Closed Principle

Extend the existing modules!

Page 31: SOLID Design principles

Open Closed Principle

How is it even possible?

Page 32: SOLID Design principles

Open Closed Principle

Depend on abstractions, new features can be added by extending the abstractions.

Page 33: SOLID Design principles

Open Closed Principle

Examples-- Drawing shapes

- Loan approval process

Page 34: SOLID Design principles

Liskov Substitution Principle

Page 35: SOLID Design principles

Liskov Subsitution Principle

”Base classes instances must be replaceable by the sub class instances without any change in the

application”

Page 36: SOLID Design principles

Liskov Subsitution Principle

Lets go back to an earlier example of Drawing shape

Page 37: SOLID Design principles

Liskov Subsitution Principle

Other examples- - Bird, Flight and Non-Flight

- Loading of settings-Rectangle and Square

Page 38: SOLID Design principles

Interface Seggregation Principle

Page 39: SOLID Design principles

Inteface Seggregation Principle

”Clients should not depend upon the interfaces they do not use”

Page 40: SOLID Design principles

Inteface Seggregation Principle

How can we pollute the interfaces? OR

How do we end up creating Fat interfaces?

Page 41: SOLID Design principles

Inteface Seggregation Principle

Door, Time and TimedDoor example. This example also violates LSP

Page 42: SOLID Design principles

Inteface Seggregation Principle

Possible Solutions Use delegation- Adapter Pattern Use multiple inheirtance- Interfaces, Mixins

Page 43: SOLID Design principles

Dependency Inversion Principle

Page 44: SOLID Design principles

Dependency Inversion Principle

”High level modules should not depend on the low level details modules, instead both should depend

on abstractions”

Page 45: SOLID Design principles

Dependency Inversion Principle

Copy

Read Keyboard Write Printer

What's GOOD or BAD about this design?

Page 46: SOLID Design principles

Dependency Inversion Principle

Copy program is NOT REUSABLE Tightly bound to Keyboard and Printer

Read Keyboard and Write Printer are REUSABLE

Page 47: SOLID Design principles

Dependency Inversion Principle

Copy program shouldn't be dependent on the Low level Read/Write modules.

How can we correct this?

Page 48: SOLID Design principles

Dependency Inversion Principle

Copy

Keyboard Reader Print Writer

Reader Writer

Copy program now depends on Abstractions- Reader and Writer

Page 49: SOLID Design principles

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

Page 50: SOLID Design principles

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