Top Banner
The OO Design Principles The OO Design Principles
33
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Solid Principle

The OO Design PrinciplesThe OO Design Principles

Page 2: Solid Principle

Agenda

Need of Design

Design Smells

2

Class Design Principles

Summary

Page 3: Solid Principle

Agenda

Need of Design

Design Smells

3

Class Design Principles

Summary

Page 4: Solid Principle

What's meaning

of design..?

What's the different

compared to

Design is

about how

What Design Exactly About is..?

4

compared to

analysis..?Analysis is

about what

Page 5: Solid Principle

Why do we

Need

Design..?

To deliver

faster

To manage

change

Why do we need design..?

5

change

To deal with

complexity

Page 6: Solid Principle

Agenda

Need of Design

Design Smells

6

Class Design Principles

Summary

Page 7: Solid Principle

Okay... I under stand the

Importance of design

How do we know a

Design is bad..?

Design Smells

7

Are they any symptoms

Of bad design...

Page 8: Solid Principle

Immobility

Viscosity

Rigidity

Fragility

Design Smells(contd…)

8

ViscosityFragility

Page 9: Solid Principle

Rigidity

� Tendency for software to be difficult to change.

The design is hard to change

9

Tendency for software to be difficult to change.

� Single change causes cascade of subsequent changes in

dependent modules.

� The more module must be changed the more rigid the

design.

Page 10: Solid Principle

Fragility

� Tendency of the software to break in many places every

The design is easy to break

10

Tendency of the software to break in many places every

time it's changed .

� The breakage occurs in areas with no conceptual

relationship

� On every fix the software breaks in unexpected way.

Page 11: Solid Principle

Immobility

� Inability to reuse software from other projects or modules .

Difficult to reuse

11

Inability to reuse software from other projects or modules .

� The useful module have to many dependencies.

� Cost of rewriting is less compared to the risk faced to

separate those part.

Page 12: Solid Principle

Viscosity

� It's easy to do the wrong thing, but hard to do the right

Hard to do the right thing

12

It's easy to do the wrong thing, but hard to do the right

thing.

� When the design preserving methods are more difficult to

use then the hacks

� When dev environment is slow and inefficient developer

will be tempted to do wrong things.

Page 13: Solid Principle

Why design becomes rigid,

Fragile Immobile and viscousIs there any

Characteristics

For good design

Design Characteristics

13

Page 14: Solid Principle

High cohesion

Improper

dependencies

Between modules

Design Characteristics

14

Low coupling

Page 15: Solid Principle

So how can we

Achieve good

Design..?

SRP

LSP

OCP

Design Characteristics

15

ISP

DIP

So let's go SOLID..

Page 16: Solid Principle

Single Responsibility Principle

� There should never be more then one reason

This is hard to see, as we think

Responsibility in group

16

There should never be more then one reason

to change a class.

� Many responsibility means many reason to

change.

Page 17: Solid Principle

1. Removes the immobility Smell from

Design.

2. Deodorizes the Rigidity Smell

Ohh.. SRP

Benefits of SRP

17

Page 18: Solid Principle

Open Closed Principle

� Software entities should be open for extension, but closed for

Abstraction is the key

18

Software entities should be open for extension, but closed for

modification.

� Keep the things that change frequently away from things that

don't change.

Page 19: Solid Principle

Open Closed Principle (contd…)

Open for Extension

� Behavior of the module

can be extended.

Closed for Modification

� The source of such a module

is invisible.

OCP is the heart of object oriented design

19

� Make the module behave

in new and different ways

as the requirement of the

application change.

� Resist making source change

to it.

Page 20: Solid Principle

Is 100%

Closer

possible

Is there any

techniques

� In Reality 100% Closer

is not attainable.

� Closer must be

Strategic.

Abstraction is the key

Further thinking of OCP

20

Flexibility, re usability and maintainability is

The benefits

Page 21: Solid Principle

Liskov's SubstitutionPrinciple

� LSP defines the inheritance principle.

Subclass should be substitutable for their base class

21

LSP defines the inheritance principle.

� It makes clear that in OO design IS-A relationship is about

behavior; behavior that clients depend on.

� If client uses a base class, then it should not differentiate

the base class.

Page 22: Solid Principle

LSP Violation Example

void g(Rectangle r)

{

r.setWidth(5);

r.setHeight(4);

if(r.getArea() != 20)

throw new

Exception("Bad area!");

Square is not

Rectangle!

22

Exception("Bad area!");

}

IS-A Relationship

Square’s behavior is

changed, so it is not

substitutable to

Rectangle

Page 23: Solid Principle

Further thinking of LSP

� In order to be substitutable, the contract of the base class

must be honored by the derived class.

Derived class substitutable or base class, if

Design by Contract

23

� Derived class substitutable or base class, if

� Preconditions are no stronger then the base class

� Preconditions are no weaker then the base class

Derived method should expect no more and provide no less

Page 24: Solid Principle

LSP Violation

The solution will likely to be put into an if else statement in the

client side.

�Are you sure it works?

�What happen if new

derivatives of rectangle

come..?

May be we've allocate

24

May be we've allocate

The responsibility

Wrongly.

It violates OCP

Page 25: Solid Principle

Interface Segregation Principle

� Client should not forced to depend on methods they do not

Many client specific interface are Better then one

general purpose interface

25

Client should not forced to depend on methods they do not

use.

� ISP deals with designing cohesive interfaces and avoiding

fat interfaces.

� What happen when the big class changes? All depending

module must also change.

Page 26: Solid Principle

Service

<<Client A methods>>

+ ...

<<Client B methods>>

+ ...

<<Client C methods>>

+ ...

Client A

Client B

An violation of ISP example

26

ISP violation

Client C

Page 27: Solid Principle

Segregated

interface

Service

<<Interface>>

Service A

<<Client A methods>>

+ ..

<<Interface>>

Service B

Client A

An violation of ISP example: Solution

27

<<Client A methods>>

+ ...

<<Client B methods>>

+ ...

<<Client C methods>>

+ ...

Service B

<<Client B methods>>

+ ..

<<Interface>>

Service C

<<Client C methods>>

+ ..

Client B

Client C

Page 28: Solid Principle

Dependency Inversion Principle

� High level module should not depend upon low level

Depend upon abstraction. Do not depend

Upon concretions

28

High level module should not depend upon low level

modules, both should depends upon abstraction

� Abstraction should not depends upon on details, details

should depends on abstraction.

Page 29: Solid Principle

DIP

A DIP example

29

DIP

violation

Page 30: Solid Principle

DIP Summary

� Inversion of dependencies is the hallmark of good object

oriented design.

� If it's dependencies are inverted, it has an OO design If it's

dependencies are not inverted it has a procedural design.

Dependency injection is the core of the famous spring

30

� Dependency injection is the core of the famous spring

framework

Hollywood principle: “ Don't call us, we'll call you”.

Page 31: Solid Principle

Summary

• A class should have only one reason to changeSRP

• A Module should be open for extension but

closed for modification.OCP

• Subclass should be substitutable for their base LSP

31

• Subclass should be substitutable for their base

class.LSP

• Many client specific interfaces are better then

one general purpose interface .ISP

• Depends upon abstraction. Do not depends

upon concretions. DIP

Page 32: Solid Principle

Questions ???Questions ???

Page 33: Solid Principle