Solid principles

34
SOLID Principles Object Oriented Programming Principles

Transcript of Solid principles

Page 1: Solid principles

SOLID Principles

Object Oriented Programming Principles

Page 2: Solid principles

SOLID Overview

SOLID Principles:

S SRP Single Responsibility Principle

O OCP Open/Close Principle

L LSP Liskov Substitution Principle

I ISP Interface Segregation Principle

D DIP Dependency Inversion Principle

Page 3: Solid principles

The Single Responsibility Principle

A class should have only one responsibility

Page 4: Solid principles

The Single Responsibility Principle

An actor for a responsibility is the single source of

change for that responsibility. (Robert C. Martin)

Page 5: Solid principles

The Single Responsibility Principle

Is this class reasonable?

What if we need to print the document to

multi formats as PDF, Printer?

Page 6: Solid principles

The Single Responsibility Principle

No “Print” method in the class

Document is just an Entity

There are multi classes to do

the printing work.

Each class is a kind of Printer

Page 7: Solid principles

The Single Responsibility Principle

Page 8: Solid principles

The Single Responsibility Principle

Each class has only one role, one responsibility

In this example

Document defines the details of document, no more

responsibility

Printer defines how to print the document

If a change of printer is required, the related printer

should be changed, document and other printers not

required to change

An actor for a responsibility is the single source of

change for that responsibility. (Robert C. Martin)

Page 9: Solid principles

The Open/Close Principle

Software entities should be open for extension, but closed for modification.

Page 10: Solid principles

The Open/Close Principle

Software entities (classes, modules, functions, etc.)

should be open for extension, but closed for

modification

Page 11: Solid principles

The Open/Close Principle

Let’s see this code

What if we need a new Shape?

Page 12: Solid principles

The Open/Close Principle

If we need a new Shape:Create a new Shape “Trapezium” with new type id

Add a new method “drawTrapezium” to the class

GraphicEditor

Modify method drawShape to support new shape

We need to modify too much to have new Shape

Page 13: Solid principles

The Open/Close Principle

Let’s see this code

No change on GraphicEditor

when a new Shape is added

To have a new Shape:

- just extend the Shape class

- override the draw method

Page 14: Solid principles

The Open/Close Principle

Page 15: Solid principles

Open for extension: Rectangle, Circle, Trapezium… extend from Shape

Closed to modification: GraphicEditor no need to change when new Shape

added

The Open/Close Principle

Software entities (classes, modules, functions, etc.)

should be open for extension, but closed for

modification

Page 16: Solid principles

The Liskov Substitution Principle

Child classes should never break the parent class type definitions.

Page 17: Solid principles

The Liskov Substitution Principle

Subtypes must be substitutable for their base types.

( Robert C. Martin)

Page 18: Solid principles

The Liskov Substitution Principle

Square is a special Rectangle

with adjacent sides equal

Page 19: Solid principles

The Liskov Substitution Principle

Let’s see this code

Square is a Rectangle

Page 20: Solid principles

The Liskov Substitution Principle

Let’s see this code

- verifyArea always throws exception

with Square

- Square is not really a Rectangle

Page 21: Solid principles

The Liskov Substitution Principle

Square is extended from Rectangle

But Square changes the characteristic of

Rectangle

So Square should not be a Rectangle

Square should not extended from Rectangle

Page 22: Solid principles

The Liskov Substitution Principle

Subtypes must be substitutable for their base types.

( Robert C. Martin)

Because Square changes the characteristic of

Rectangle, so Square should not extend from

Rectangle

Page 23: Solid principles

The Interface Segregation Principle

Clients should not be forced to depend upon interfaces that they don't use.

Page 24: Solid principles

The Interface Segregation Principle

The interface-segregation principle (ISP) states that

no client should be forced to depend on methods it

does not use.

Page 25: Solid principles

The Interface Segregation Principle

Page 26: Solid principles

The Interface Segregation Principle Let’s see this code

Workers needs to work

and eat also

But the Manager just

needs to take care how

they work

Page 27: Solid principles

The Interface Segregation Principle

Worker still works and eats Robot just works, no need to

eat

By this way, Manager doesn’t need to know about the eat method that he doesn’t care

Page 28: Solid principles

The Interface Segregation Principle

The interface-segregation principle (ISP) states that

no client should be forced to depend on methods it

does not use.

Because Manage doesn’t need to know abt how the

worker eats. The best way is IWorker doesn’t contain

eat method

Page 29: Solid principles

The Dependency Inversion Principle

Depend upon Abstractions. Do not depend upon concretions.

Page 30: Solid principles

The Dependency Inversion Principle

A. High-level modules should not depend on low-

level modules. Both should depend on abstractions.

B. Abstractions should not depend upon details.

Details should depend upon abstractions.

Page 31: Solid principles

The Dependency Inversion Principle Let’s see this code

AppManager depends on

EventLogWriter

If we need another way to

notify as Send SMS, Send

Mail, we need to change

AppManager

Page 32: Solid principles

The Dependency Inversion Principle Update the code

Page 33: Solid principles

The Dependency Inversion Principle

Every classes depend on INotificationActions

AppManager doesn’t depend on

Lower-level modules as

EventLogWriter, EmailSender,

SMSSender

INotificationAction

depends on Nothing

Page 34: Solid principles

Reference

http://code.tutsplus.com/series/the-solid-principles--cms-634

http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

http://www.oodesign.com/design-principles.html