object design - irisapeople.irisa.fr/francois.schwarzentruber/mit2_cvfp_2013/object_desi… ·...

73
Object design François Schwarzentruber ENS Cachan – Antenne de Bretagne

Upload: others

Post on 05-Apr-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Object design

François SchwarzentruberENS Cachan – Antenne de Bretagne

Page 2: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Outline

● Symptoms of rotting systems● Principles of object oriented class design● Principles of Package Architecture● Dreams

Page 3: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Outline

● Symptoms of rotting systems● Principles of object oriented class design● Principles of Package Architecture● Dreams

Page 4: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Symptoms of rotting systems (according to Robert C. Martin)

Four possible unsuitable behaviors of the developer team:

● Rigidity

● Fragility

● Immobility

● Viscosity

Page 5: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Rigidity

System

Add functionality 1

Add functionality 2

Add functionality 3

Page 6: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Rigidity

Could you please addthe undo feature?

Yes I will implement it

Page 7: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Rigidity

Page 8: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Rigidity

Ok... let us forgetabout it...

I am sorry... it willtake several months

to do it!

This functionality will never be implemented.

Page 9: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Fragility

Add functionality

Page 10: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Fragility

Could you please addthe undo feature?

Yes I will implement it.

Page 11: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Fragility

Page 12: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Fragility

Oh :(But you already changed 3 times

the structure of the softwarefor all the 3 featureswe have told about!

I have done it!1) I completely changed the structure.

2) I implemented it...

The developer team is not to be trusted.

Page 13: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Immobility

rewrite

AlreadyExistingpackage

reuse

Page 14: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Immobility

I need a scheme evaluator...

Oh! Here is a completescheme library for JAVA.

SchemeJ

A completescheme library

for JAVA !

Opensource

>(+ 1 2)3

Page 15: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Immobility

I will try to findthe scheme evaluator from it...

SchemeJ

A completescheme library

for JAVA !

Opensource

Page 16: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Immobility

I do not understand anything.

SchemeJ

A completescheme library

for JAVA !Open

source

I will write my own interpreter...

Page 17: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Viscosity

system

hack

Apply gooddesign principles

Page 18: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Viscosity

Page 19: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Viscosity - example

Extract from a MIT2 internship report

Page 20: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Outline

● Symptoms of rotting systems● Principles of object oriented class design● Principles of Package Architecture● Dreams

Page 21: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

SOLID

● 5 principles of object oriented class design

● Introduced by Robert Cecil Martin

Page 22: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

S : Single responsibility principle

There should never be more than one reason for aclass to change.

● Class of a game:

- that computes the position of enemies

- that computes the score

● Class of a game that uses two objects:

- one that computes the position of enemies

- another that computes the score

Change the gravity ?

Change the way ofcounting the score ?

Page 23: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

S : Single responsibility principle

There should never be more than one reason for aclass to change.

Change the gravity ?

Change the way ofcounting the score ?

I do gravity... After your modification,I modify the

score definition.

Or I implement both...

I modify GravityManager. I modify

ScoreManager.

Page 24: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

S : Single responsibility principle

There should never be more than one reason for aclass to change.

Change the gravity ?

Change the way ofcounting the score ?

I proposetwo gravity modes...

I proposetwo scoremodes...

Page 25: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

O : Open Closed Principle

● Change the code source of module to add functionnality

● Being able to extend modules without changing the code source

→ abstraction

Page 26: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

O : Open Closed Principle

Class Enemy{

void move(){

if(type == RABBIT)...

else if(type == BROWSER)...

else if(type == MUSHROOM)...

}}

Page 27: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

O : Open Closed Principle

Class Enemy{

void move(){

if(type == RABBIT)...

else if(type == BROWSER)...

else if(type == MUSHROOM)...

}} For each change of

a type of enemy, I recompile all

the class Enemy...

Oh I just recompileone class...

Page 28: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

O : Open Closed Principle

Class Enemy{

void move(){

if(type == RABBIT)...

else if(type == BROWSER)...

else if(type == MUSHROOM)...

}

void jump(){

if(type == RABBIT)...

else if(type == BROWSER)...

else if(type == MUSHROOM)...

} . . .}

If I add a new type of enemy,

I check all the if/elsestatements...

Oh I just adda new class...

Page 29: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

L : Liskov Substitution Principle

Barbara LiskovTuring award 2008

or

?

Page 30: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

L : Liskov Substitution Principle

Page 31: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

L : Liskov Substitution Principle

But a circle is simplier...And we extend it to

make a ellipse...It seems reasonable...

Page 32: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

L : Liskov Substitution Principle

But a circle is simplier...And we extend it to

make a ellipse...

Class Circle{

public float getR();public float getArea();

}

Class Ellipse extends Circle{

...}

Circle c;c = new Ellipse(...);

/* Here we expect thatthe area of c isc.getR()^2 * PI*/

Page 33: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

L : Liskov Substitution Principleand design by contract

Class Ellipse{

public float getR1();public float getR2();public float getArea();

}

Class Circle extends Ellipse{

...}

Ellipse e;e = new Circle(...);

/* Here we expect thatthe area of e isc.getR1() * c.getR2() * PI*/

Page 34: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

L : Liskov Substitution Principleand design by contract

Class Ellipse{

invariant: inv

precondition: prepostcondition: posvoid f(Point p1, p2){}:

}

Class Circle extends Ellipse{

invariant: stronger than inv

precondition: weaker than prepostcondition: stronger than posvoid f(Point p1, p2){}:

}

Page 35: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

L : Liskov Substitution Principleand design by contract

Class Ellipse{

void setFocus(Point p1, p2){

this.p1 = p1;this.p2 = p2;

}:

}

Class Circle extends Ellipse{

void setFocus(Point p1, p2){

this.p1 = p1;this.p2 = p1;

}:

}

:

Page 36: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

A little problem

Class Ellipse{

postcondition: this.p1 == p1 & this.p2 == p2

void setFocus(Point p1, p2){

this.p1 = p1;this.p2 = p2;

}:

}

Class Circle extends Ellipse{

void setFocus(Point p1, p2){

this.p1 = p1;this.p2 = p1;

}:

}

:

!

Ellipse e = new Circle();e.setFocus(p1, p2);

assert(e.getP1() == p1);assert(e.getP2() == p2);

Page 37: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

I : Interface Segregation Principle

I am plumber...

I am an electrical fitter.

Here is the entire mapof the house.

I am plumber...

I am an electrical fitter.

I propose an interfacefor accessing to pipes.

I propose an interfacefor accessing to cables.

Page 38: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

I : Interface Segregation Principle

I am plumber...

I am an electrical fitter.

Here is the entire mapof the house.

I am plumber...

I am an electrical fitter.

I propose an interfacefor accessing to pipes.

I propose an interfacefor accessing to cables.

I understand better.I do not understand.

Page 39: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

I : Interface Segregation Principle

I am plumber...

I am an electrical fitter.

Here is the entire mapof the house.

I am plumber...

I am an electrical fitter.

I propose an interfacefor accessing to pipes.

I propose an interfacefor accessing to cables.

Oh no...

I do not understand the map.

I changed the pipes...

OK.

Page 40: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

D: Dependency Inversion Principle

I make a web servicefor booking flights.

And what about trains?

I make a web servicefor booking abstract

travels.

Perfect!

Ok... I will implementit for flights.

I propose an interfacethat specifies

what an abstract travel is.Cool... I like flights.

Ok... I will implementit for trains.

Oh no...

Page 41: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

D: Dependency Inversion Principle

Example:

● JAVA MidiSound

MIDIPlayer I_Synthesizer

Synthesizer Mechanical Piano

Page 42: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Outline

● Symptoms of rotting systems● Principles of object oriented class design● Principles of Package Architecture● Dreams

Page 43: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Outline

● Symptoms of rotting systems● Principles of object oriented class design● Principles of Package Architecture

● Inside a package● Between packages

● Dreams

Page 44: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Remark

We refactor the packages during the development:

● At the beginning stage, we favor the developer

● At the end, we favor the clients.

Page 45: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The Release Reuse Equivalency Principle

A package

● the granule of reuse

● the granule of release

● Number of versions

● Should support and maintain older versions

Goodfor the client!

1.12 1.13 2.0

Page 46: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The Common Closure Principle

Classes that change together, belong together.

And we need to change here too!

Change !!

Goodfor the

developer!

Packages tend to be large.

Page 47: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The Common Closure Principle

Classes that change together, belong together.

And we need to change here too!

Change !!

Goodfor the

developer!

Packages tend to be large.

dev

Mess for testing, etc.

Page 48: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The Common Reuse Principle

Classes that aren’t reused together should not be grouped together.

Change !!

client

Oh no... you modifya class I don't care

…But I must still test myimplementation that

depends on your $%!$ package !

client

Change !!

Goodfor the client!

Packages tend to be small.

client

Page 49: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Cohesion

Page 50: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Outline

● Symptoms of rotting systems● Principles of object oriented class design● Principles of Package Architecture

● Inside a package● Between packages

● Dreams

Page 51: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Coupling

Page 52: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The Acyclic Dependencies Principle

The dependencies between packages must not form cycles.

I work on Protocol...and I need to testmy package with

Comm Error.

Page 53: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The Acyclic Dependencies Principle

The dependencies between packages must not form cycles.

I work on Protocol...and I need to testmy package withall the packages !

Page 54: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Solution: Dependency Inversion Principle

Page 55: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Solution: Dependency Inversion Principle

GUI

MessageManager

Comm Error

ConcreteMessageDisplayer

I_MessageDisplayer MessageManager

ErrorManager

Page 56: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The Stable Dependencies Principle

Depend in the direction of stability

My work dependson package X !

I need to modify X..because it is related to

other packages...because it is a difficult

part of the project...

Oh no... X is not stable...

My work dependson package X !

Good point. X will notChange anymore.

X is stable !

Page 57: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Stable / instable

Y instableX stable

Page 58: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The stable abstractions principle

Stable packages should be abstract packages.

Flexible / instable

Stable

Page 59: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The stable abstractions principle

Stable packages should be abstract packages.

Flexible / instable

Stable→ but we want them flexible→ should be abstract in order to be extended!

Page 60: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Outline

● Symptoms of rotting systems● Principles of object oriented class design● Principles of Package Architecture● Dreams

Page 61: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Dream 1: Automated assistance

Software to help you to design the architecture X

warning:no abstractenough

Page 62: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Measuring instability

i

Page 63: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Measuring abstractness

Page 64: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The zone of pain: stable and too concrete

Graphical libraryfor Android 3.0

Map Player

Page 65: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The main sequence: stability = abstractness

GeneralGraphical library

Map Player

Page 66: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

The zone of uselessness: abstract but not used!

GeneralGraphical library

Page 67: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Instability VS Abstractness

Page 68: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Dream 2: creating automatically the packages partition

Graph of dependencies G = (V, E)

Page 69: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Dream 2: creating automatically the packages partition

Page 70: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Dream 2: creating automatically the packages partition

A new field

● [Mitchell 2002]

● Bunch [Mitchell et al. 2006]

● Nothing about stability and abstractness

● Preliminary work...

Page 71: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Related problems

P:

● Minimal cut by flow algorithms

= finding two packages with low coupling

NP:

● Graph partitioning (minimal cut plus a constraint over the size of the packages)

= finding two `big' packages with low coupling

● The clique problem, NP-complete

= find a package with high cohesion

Page 72: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Mitchell's PhD

● Measuring cohesion

● Measuring coupling

● Measuring the quality of a clustering

Page 73: Object design - IRISApeople.irisa.fr/Francois.Schwarzentruber/mit2_cvfp_2013/object_desi… · Object design François ... Class of a game that uses two objects: - one that computes

Heuristics

● Hill-climbing algorithms

● Genetic algorithms

PS: People claim the problem is NP-complete (I want a proof)