Design Principles iwongu at gmail dot com. What is Object-Oriented design?

97
Design Principles iwongu at gmail dot com

Transcript of Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Page 1: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Design Principles

iwongu at gmail dot com

Page 2: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

What is Object-Oriented design?

Page 3: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Dependency Management

Page 4: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

First VersionAll designs start well

void copy(){

int ch;while ( (ch = ReadKeyboard()) != EOF)

WritePrinter(ch);}

Page 5: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Second VersionOh, no! Nobody said the requirements might change!

bool gTapeReader = false; // remember to reset this flag

void copy(){

int ch;while ( (ch = gTapeReader ? ReadTape() : ReadKeyboard()) != EOF)

WritePrinter(ch);}

Page 6: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Third VersionHow unexpected! Requirements changed again!

bool gTapeReader = false; bool gTapePunch = false;// remember to reset these flags

void copy(){

int ch;while ( (ch = gTapeReader ? ReadTape() : ReadKeyboard()) != EOF)

gTapePunch ? WritePunch(ch) : WritePrinter(ch);}

Page 7: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Won-derful

Use

Change

Rot

Smell

Re-design

Page 8: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Design SmellsThe odors of rotting software

1. It’s rigid.2. It’s fragile.3. It’s not reusable.

Page 9: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

RigidityRigidity is the inability to be changed

1. The impact of a change cannot be predicted.

2. If not predicted, it can not be esti-mated.

3. Time and cost can not be qualified.4. Managers become reluctant to au-

thorize change.

Page 10: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

FragilitySoftware changes seem to exhibit non-local effects.

1. A single change requires a cascade of subsequent changes.

2. New errors appear in areas that seem unconnected to the changed areas

3. Quality is unpredictable.4. The development team loses credi-

bility.

Page 11: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

ImmobilityIt's not reusable.

1. Desirable parts of the design are dependent on undesirable parts.

2. The work and risk of extracting the desirable part may exceed the cost of redeveloping from scratch.

Page 12: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Example of a good designFirst and only version!

void copy(FILE* in, FILE* out){

int ch;while ( (ch = fgetc(in)) != EOF)

fputc (ch, out);}

But, wait! Aren't we supposed to be learning OO design? This isn't OO, is it?

Page 13: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

… Is it?It's a small program based on abstraction!

1. FILE is an abstraction.a. It represented some kind of byte stream.b. It has many variations.

2. It has methods.a. The methods are dynamically bound.

FILE is a class, just implemented differ-ently.

Page 14: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Rephrased in OOFirst and only version!

interface Reader{ char read(); }

interface Writer{ void write(char c); }

public class Copy{

Copy(Reader r, Writer w){

itsReader = r;itsWriter = w;

}public void copy(){

int c;while ( (c = itsReader.read()) != EOF )

itsWriter.write(c);}

Reader itsReader;Writer itsWriter;

}

Page 15: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

CHANGE

Page 16: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

“CHANGE”

The one constant in software devel-opment

Page 17: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

“Belady and Lehman’s Laws”

Software will continually change.

Software will become increasingly unstructured as it is changed.

Page 18: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Ities of Software Quality

• Reliability• Efficiency• Readability• Understandability• Modifiability, Maintainability• Testability• Portability

Page 19: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

UMLUnified Modeling Language

Page 20: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Class

Customer

-name: String-address: String

+creditRating()

Page 21: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Association

Customer

-name: String-address: String

+creditRating()

Order

-dateReceived: Date-number: String-price: Money

+dispatch()+close()

1*

Page 22: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Generalization

Customer

-name: String-address: String

+creditRating()

Order

-dateReceived: Date-number: String-price: Money

+dispatch()+close()

Corporate Customer

-contactName: String-creditRating: String-creditLimit: Double

+remind()+billForMonth()

Personal Customer

-creditCard#: long int

1*

Page 23: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Dependency

Customer

-name: String-address: String

+creditRating()

Order

-dateReceived: Date-number: String-price: Money

+dispatch()+close()

Corporate Customer

-contactName: String-creditRating: String-creditLimit: Double

+remind()+billForMonth()

Personal Customer

-creditCard#: long int

Date

Money

1*

Page 24: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

UML Class Diagram shows the relationships of Classes.

Dependency

Association

Aggregation

Composition

Generalization

Realization

Page 25: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Design Principles

Page 26: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Design Principles

SRP Single Responsibility Principle OCP Open Closed Principle LSP Liskov Substitution Principle DIP Dependency Inversion Principle ISP Interface Segregation Prin-

ciple

Page 27: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

SRPSingle Responsibility Princi-

ple

Page 28: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

There should never be more than one reason for a class

to change.

Page 29: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

In the context of the SRP,a responsibility means "a

reason for change."

Page 30: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Each responsibility is an axis of change.

Page 31: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Orthogonalclass Y

class X

No need to change

Page 32: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Non-Orthogonalclass Y

class X

Need to change

Page 33: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

SRP Violation

Rectangle

+draw()+area(): double

Computational Geometry Application Graphical Application

GUI

Page 34: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

SRP

Computational Geometry Application

Graphical Application

GUI

Geometric Rectangle

+area(): double

Rectangle

+draw()

Page 35: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

But, it’s not easy to see SRP.

Modem

+dial()+hangup()+send()+recv()

Page 36: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

But, it’s not easy to see SRP.

Modem

+dial()+hangup()+send()+recv()

Data Channel<<interface>>

+send()+recv()

Connection<<interface>>

+dial()+hangup()

Page 37: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Question?

Page 38: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

OCPOpen-Closed Principle

Page 39: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Software entities should be open for extension

but closed for modification.

Page 40: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

But how?

Page 41: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Abstraction is the key.

Page 42: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

enum ShapeType { circle, square }; struct Shape { ShapeType itsType; };

struct Circle { ShapeType itsType;

}; struct Square {

ShapeType itsType; };

void DrawAllShapes(Shape* list[], int n) { for (int i = 0; i < n; ++i) {

Shape* s = list[i]; switch (s->itsType) { case square:

DrawSquare((Square*)s); break;

case circle: DrawCircle((Circle*)s); break;

} }

}

Page 43: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

struct Shape {virtual void Draw() const = 0;

};struct Square : Shape {

virtual void Draw() const;};struct Circle : Shape {

virtual void Draw() const;};

void DrawAllShapes(Shape* list[], int n) {for (int i = 0; i < n; ++i) {

Shape* s = list[i];s->Draw();

}}

Page 44: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

OCP Violation

If new shapes are needed,

Square

Circle

ShapeDrawAllShapes

Page 45: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

OCP Violation

Circle

Square

DrawAllShapes

Triangle

Pentagon

Shape

Page 46: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

OCP

If new shapes are needed,

Circle Square

DrawAllShapes Shape

Page 47: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

OCP

Circle Square

DrawAllShapes

Triangle Pentagon

Shape

Page 48: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

But, OCP is not just inheri-tance.

Page 49: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

OCP is the root motivationbehind many of the heuris-

tics and conventions.

For example,

Page 50: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Make All Member Variables Private.

Page 51: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

No Global Variables – Ever.

Page 52: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

RTTI is Dangerous.

Page 53: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Question?

Page 54: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

LSPLiskov Substitution Principle

Page 55: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Subtypes should be substi-tutable for their base types.

Page 56: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Rectangle

Rectangle

-top_left: Point-width: double-height: double

+set_width(w: double)+set_height(h: double)+get_width(): double+get_height(): double

Page 57: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Square IS-A rectangle.

Page 58: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Square

Rectangle

-top_left: Point-width: double-height: double

+set_width(w: double)+set_height(h: double)+get_width(): double+get_height(): double

Square

Page 59: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

void Square::set_width(double w){

Rectangle::set_width(w);Rectangle::set_height(w);

}

void Square::set_height(double h){

Rectangle::set_width(h);Rectangle::set_height(h);

}

Page 60: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

double g(Rectangle& r){

r.set_width(5);r.set_height(4);

assert(r.area() == 20);}

Page 61: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Square IS-A rectangle.But,

Square is NOT substitutable for rectangle.

Page 62: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Violating the LSP often re-sults in the use of Run-Time

Type Information (RTTI).

Page 63: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

double g(Rectangle& r){

r.set_width(5);r.set_height(4);

if (dynamic_cast<Square*>(&r) != 0){

assert(r.area() == 16);}else{

assert(r.area() == 20);}

}

Page 64: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

It’s also a violation of OCP.

Rectangle

-top_left: Point-width: double-height: double

+set_width(w: double)+set_height(h: double)+get_width(): double+get_height(): double

Square

g

Page 65: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Using DBC, LSP means,

Page 66: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

DBC?

Design By Contract.

PreconditionPostcondition

Invariant

Page 67: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

A routine redeclaration may only replace the original

precondition by one equal or weaker, and the original postcondition by one equal

or stronger.

Page 68: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Base

Derived

Page 69: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

The postcondition of Square::set_width() is

weaker than the postcondi-tion of

Rectangle::set_width().

Page 70: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Rectangle

Square

Page 71: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Question?

Page 72: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

DIPDependency Inversion Prin-

ciple

Page 73: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

a. High-level modules should not depend on low-level modules. Both should

depend on abstractios.

Page 74: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

b. Abstractions should not depend on details. Details should depend on abstrac-

tions.

Page 75: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Structured Design

Policy Layer

Mechanism Layer

Utility Layer

Page 76: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Dependency Inversion

Policy Layer

Mechanism Layer

Utility Layer

Policy Service Interface<<interface>>

Mechanism Service Interface<<interface>>

Page 77: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Dependency Inversion

Utility

Mechanism

Policy

Policy Layer

Mechanism Layer

Utility Layer

Policy Service Interface<<interface>>

Mechanism Service Interface<<interface>>

Page 78: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Depend on abstractions.

Page 79: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

No variable should hold a pointer or reference to a con-crete class.

No class should derive from a concrete class.

No method should override an implemented method of any of its base classes.

Page 80: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

But, it’s impossible.

For example, someone has to create the instances of the concrete class, and

whatever module does that will depend on them.

Page 81: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

And, it might not be a prob-lem to depend on concrete

but non-volitile classes.

Page 82: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

DIP Violation

Button

+poll()

Lamp

+turn_on()+turn_off()

Page 83: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

DIP

Button

+poll()

ButtonServer<<interface>>

+turn_on()+turn_off()

Lamp

Page 84: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Question?

Page 85: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

ISPInterface Segregation Prin-

ciple

Page 86: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Door

And, we need a timed door.

Door<<interface>>

+lock()+unlock()+is_open()

Page 87: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Timer

Timer

+register()

TimerClient<<interface>>

+timeout()0..*

Page 88: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

The first solution

What are problems?

Door<<interface>>

+lock()+unlock()+is_open()

Timer

+register()

TimerClient<<interface>>

+timeout()0..*

TimedDoor

Page 89: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Not all varieties of Door need timing.

Violation of LSP.

Page 90: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

The interface of Door has been polluted with a

method that it does not re-quire.

Fat Interface.

Page 91: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

ISPInterface Segregation Prin-

ciple

Clients should not be forced to depend on methods that

they do not use.

Page 92: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Multiple inheritance

Door<<interface>>

+lock()+unlock()+is_open()

Timer

+register()

TimerClient<<interface>>

+timeout()0..*

TimedDoor

Page 93: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Delegation

Door<<interface>>

+lock()+unlock()+is_open()

Timer

+register()

TimerClient<<interface>>

+timeout()0..*

TimedDoor

DoorTimer Adapter

Page 94: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

Question?

Page 95: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

References

Page 96: Design Principles iwongu at gmail dot com. What is Object-Oriented design?
Page 97: Design Principles iwongu at gmail dot com. What is Object-Oriented design?

http://objectmentor.com/http://objectmentor.com/resources/omi_reports_index.htmlhttp://objectmentor.com/resources/publishedArticles.html → Robert C. Martin