Object Oriented Design And Programing

Post on 12-Jan-2015

2.228 views 6 download

Tags:

description

Polymorphism Vs Enumerated

Transcript of Object Oriented Design And Programing

Object Oriented Design (OOD)in software industry

Available at www.elfuchs.com

Emmanuel FUCHS

2

31/0

3/03

Why OOD ?

Design for changesProcedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented DesignObject Oriented DesignDesign Pattern

3

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

4

31/0

3/03

Changes Request : flexible solution

Productive programmerInitial Request

1 Day After

Unproductive programmer

1 Week After

5

31/0

3/03

Changes Request : flexible solution

Productive programmerEvolution

RequestInitial Request

1 Day After

Unproductive programmer

1 Week After

6

31/0

3/03

Changes Request : flexible solution

Productive programmerEvolution

RequestInitial Request

1 Day After

1 Week After

Unproductive programmer

1 Day After

7

31/0

3/03

Changes Request : flexible solution

Productive programmerEvolution

RequestInitial Request

1 Day After

1 Week After

Few Weeks Later

Unproductive programmer

1 Day After

8

31/0

3/03

Changes Request : flexible solution

Productive programmer

Unproductive programmer

Evolution Request

Initial Request

1 Day After

1 Week After

Few Weeks Later

1 Day After

9

31/0

3/03

Changes Request : flexible solution

Unproductive programmerEvolution

RequestInitial Request

1 Day After

1 Week After

Few Weeks Later

Productive programmer

1 Day After

10

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

11

31/0

3/03

Changes Sources During Development

Requirements :Customers Discover What they Really Want During or at the End of Developments

TechnologyPerformances Are Increasing With Time

SkillWe Learn and Understand the Problem and We Discover the Right Solution on the Job

Short Term PoliticNo Comments

12

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

13

31/0

3/03

program in the future tense.

Initial Request

1 Day After

Present tenseprogramming Short term

Medium to long term1 Week After

Future tenseprogramming

14

31/0

3/03

program in the future tense.

Initial Request Evolution Request

1 Week After

Few Weeks Later

1 Day After

Present tenseprogramming

1 Day After

Future tenseprogramming

15

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

16

31/0

3/03

Object VS Procedural

ProceduralThe code solution “structure” is the problem “structure”.When problems “changes” the code structure changes.

ObjectThe solution is based on integration and collaboration of independent entities (component).An entity is a code subset.Integration and Collaboration are managed by tools (compiler).When the problems “changes” the collaboration scheme changes not entity’s code.

17

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

18

31/0

3/03

User Input

Operation:

A: Addition

B: Subtraction

C: Division

E: Multiplication

Enter a choice =>

19

31/0

3/03

User Input

Operation: Addition

A: First Integer

B: Second Integer

Enter a choice =>

20

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

21

31/0

3/03

Procedural Programming

Proc A1()

Proc a ()

CH = 1

Proc b ()

CH = 2

Proc c ()

CH = 3

CharacterInput CH

Proc A2() Proc A3()

Procedure A ()

CH = A

Proc d ()

CH = 1

Proc e ()

CH = 2

Proc f ()

CH = 3

CharacterInput CH

Procedure B ()

CH = B

Proc g ()

CH = 1

Proc h ()

CH = 2

CharacterInput CH

Procedure C()

CH = C

CharacterImput CH

case

case

case

case

case

22

31/0

3/03

Hierarchical Programming

Tapez un nom iciTapez un titre de fonction ici

Tapez un nom iciTapez un titre de fonction ici

Tapez un nom iciTapez un titre de fonction ici

Tapez un nom iciTapez un titre de fonction ici

23

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

24

31/0

3/03

User Input : Operation in R and C

Operation:

A: Real

B: Complex

Enter a choice =>

25

31/0

3/03

User Input

Operation: Complex Addition

A: First Number real part

B: First Number imaginary part

C: Second Number real part

D: Second Number imaginary part

Enter a choice =>

26

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

27

31/0

3/03

Changes Request : flexible solution

Initial Request Evolution Request

1 Day After

1 Week After

Few Weeks Later

1 Day After

28

31/0

3/03

Hierarchical, Structural Programming, “Top Down”

Reuse of Real operations for Complex Operations

Procedures

29

31/0

3/03

Hierarchical, Structural Programming, “Top Down”

Reuse of Real operations for Complex Operations

Procedures

30

31/0

3/03

Hierarchical, Structural Programming, “Top Down”

Reuse of Real operations for Complex Operations

Procedures

Spaghetti Plate

31

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

32

31/0

3/03

State and Procedure dichotomy

State Procedures

33

31/0

3/03

State and Procedure dichotomy

State Procedures

34

31/0

3/03

State and Procedure dichotomy

State Procedures

Spaghetti Plate

35

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

36

31/0

3/03

Object Paradigm

GOF definition for object: A run-time entity that packages both data and the procedures that operate on that data.

ObjectObject

OperationOperation

OperationOperation

OperationOperation

OperationOperation

Data

Attribute Interfaces

GoF stand for Gang of Four. It refers to the famous books of Vlisside and Co. Design Patterns: Elements of Reusable Object-Oriented Software.

37

31/0

3/03

Object Paradigm

GOF definition for object: A run-time entity that packages both data and the procedures that operate on that data.

UML class

ObjectObjectName

Attribute

OperationOperationOperationOperation

OperationOperation

OperationOperation

OperationOperation

OperationOperation

Data

Attribute InterfacesUML: Unified Modelling Language

GoF stand for Gang of Four. It refers to the famous books of Vlisside and Co. Design Patterns: Elements of Reusable Object-Oriented Software.

38

31/0

3/03

Object analogy

A driver doesn't care of engine's internal working.He only knows the interface

Implementation Interface

39

31/0

3/03

Object analogy

A driver doesn't care of engine's internal working.He only knows the interface

Implementation Interface

40

31/0

3/03

Object analogy

A driver doesn't care of engine's internal working.He only knows the interface

Implementation Interface

41

31/0

3/03

Polymorphism

Interface

(specification) Implementation

(body)

42

31/0

3/03

Object Interface

UML class

Name

Attribute

OperationOperationOperationOperation

Object Interface

Client

43

31/0

3/03

Object Interface and Implementation

Name

Attribute

OperationOperationOperationOperation

UML class

Object Interface

Client

Interface Implementation

44

31/0

3/03

Object Interface and Implementation

Name

Attribute

OperationOperationOperationOperation

UML class

Object Interface

Client

Interface Implementation

45

31/0

3/03

Object collaboration

46

31/0

3/03

OOD hides the problem space

With OOD likely to change aspects are encapsulated and hidden to other objects.

How and What are separated

The problem : What

The solution : How

47

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

48

31/0

3/03

The problem is in the User Input

Colour

A: Black,B: Brown,C: Red, D: Orange,E: Yellow,F: Green, G: Blue,

Enter a colour =>

49

31/0

3/03

Replace case and enum by object .

enum Color {

Black,Brown,Red, Orange,Yellow,Green,Blue,

}

50

31/0

3/03

structural switch

static void printsColor(int Value) {

switch(Value) {

case Black : processing 1

case Brown:processing 2

case Red:processing 3

case Orange:processing 4

case Yellow:processing 5

case Green:processing 6

case Blue:processing 7

} }

51

31/0

3/03

structural switch

static void flightUpdate(intstatus) {

switch(status) {

case NIL_EXIT_STATE: processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} } Software

Module BSoftwareModule A Software

Module D

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

SoftwareModule C

SoftwareModule E

52

31/0

3/03

The problem space and the solution space.

If the switch value reflects the problem for example user inputs.

When the user request new case you have to change everywhere you use the switch value.

53

31/0

3/03

Example 3: The problem is in the User Input

Colour

A: Black,B: Brown,C: Red, D: Orange,E: Yellow,F: Green, G: Blue,H: NewColor,

Enter a colour =>

54

31/0

3/03

Replace case and enum by object .

enum Color {

Black,Brown,Red, Orange,Yellow,Green,Blue,NewColor,

}

55

31/0

3/03

structural switchstatic void printsColor(int Value) {

switch(Value) {

case Black : processing 1

case Brown:processing 2

case Red:processing 3

case Orange:processing 4

case Yellow:processing 5

case Green:processing 6

case Blue:processing 7

case NewColor,processing 7

} }

56

31/0

3/03

structural switch

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(intstatus) {

switch(status) {

case NIL_EXIT_STATE: processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

SoftwareModule A

SoftwareModule B

SoftwareModule C

SoftwareModule D

SoftwareModule E

Changes

57

31/0

3/03

structural switch

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(intstatus) {

switch(status) {

case NIL_EXIT_STATE: processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

static void flightUpdate(int status) {

switch(status) {

case NIL_EXIT_STATE : processing 1

case FLIGHT_ACTIVATION_PROPOSAL:processing 2

case FLIGHT_ACTIVATION_ALARM:processing 3

case FLIGHT_ACTIVATION_CONFIRMED:processing 4

case HANDOVER_TRANSFERED,processing 5

case COORDINATION_TERMINATED,processing 6

case UNKNOWN_EXIT_STATEprocessing 7

} }

SoftwareModule A

SoftwareModule B

SoftwareModule C

SoftwareModule D

SoftwareModule ESpaghetti Plate

Changes

58

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

59

31/0

3/03

Changes Request : flexible solution

Initial Request Evolution Request

1 Day After

1 Week After

Few Weeks Later ?

1 Day After

60

31/0

3/03

The object solution

Polymorphism

61

31/0

3/03

Polymorphism

Print()Color

+print()

Client

62

31/0

3/03

Polymorphism

Color

+print()

No Changes

Print()Client

Black

+ print()

63

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

No Changes

Print()Client

Black

+ print()

64

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

No Changes

Print()Client

Black

+ print()

Blue

+ print()

65

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

No Changes

Print()Client

Black

+ print()

Blue

+ print()

newColor

+ print()

66

31/0

3/03

Object Polymorphism

Code example

67

31/0

3/03

Polymorphism

Print()Color

+print()

Client

68

31/0

3/03

Polymorphism

Color

+print()

Print()Client

Black

+ print()

69

31/0

3/03

Code of the Black object print() operation

class Black extends Color

{

public void print()

{

System.out.println( " Black "); }

}

70

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Print()Client

Black

+ print()

71

31/0

3/03

Code of the Brown object print() operation

class Brown extends Color

{

public void print()

{

System.out.println( " Brown "); }

}

72

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

Print()Client

Black

+ print()

Blue

+ print()

73

31/0

3/03

Code of the Red object print() operation

class Red extends Color

{

public void print()

{

System.out.println( " Red "); }

}

74

31/0

3/03

Code of the Blue object print() operation

class Blue extends Color

{

public void print()

{

System.out.println( " Blue "); }

}

75

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

76

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

Print()Client

Black

+ print()

Blue

+ print()

77

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

No Changes

Print()Client

Black

+ print()

Blue

+ print()

newColor

+ print()

78

31/0

3/03

Code of the Blue object print() operation

class NewColor extends Color

{

public void print()

{

System.out.println( " NewColor "); }

}

79

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

80

31/0

3/03

GOF

GoF stand for Gang of Four. It refers to the pattern seminal book of John Vlissides, Erich Gamma, Richard Helm, Ralph Johnson:

Title: Design Patterns: Elements of Reusable Object-Oriented Software.

81

31/0

3/03

GOF: The book cover

82

31/0

3/03

Design Pattern GOF Definition

A design pattern systematically names, motivates, and explains ageneral design that addresses a recurring design problem inobject-oriented systems. It describes the problem, the solution, when to apply the solution, and its consequences. It also gives implementation hints and examples. The solution is a general arrangement of objects and classes that solve the problem. The solution is customized and implemented to solve the problem in a particular context.

83

31/0

3/03

Why Design Pattern ?

Because we want to use polymorphism to manage the changesBut it is very difficult to find the object class lattice which leads to polymorphismDesign pattern Language is a catalogue of object class lattices which handle a certain problem with polymorphism.

84

31/0

3/03

What to Expect from Design Patterns

A Common Design VocabularyA Documentation and Learning AidAn Adjunct to Existing MethodsA Target for Refactoring

Anti pattern are also useful.

85

31/0

3/03

Design Pattern Catalogue

Creational PatternsAbstract Factory Builder Factory Method Prototype Singleton

86

31/0

3/03

Design Pattern Catalogue

Structural PatternsAdapter Bridge Composite Decorator Facade Flyweight Proxy

87

31/0

3/03

Design Pattern Catalogue

Behavioral PatternsChain of Responsibility Command Interpreter Memento Iterator Mediator Observer State Strategy Template Method Visitor

88

31/0

3/03

Design Pattern Map

89

31/0

3/03

What to Expect from Design Patterns

A Common Design VocabularyA Documentation and Learning AidAn Adjunct to Existing MethodsA Target for Refactoring

Anti pattern are also useful.

90

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

91

31/0

3/03

The GOF Abstract Factory Design Pattern

*GoF stand for Gang of Four. It refers to the famous books of John Vlissides, Erich Gamma, Richard Helm, Ralph Johnson. Design Patterns: Elements of Reusable Object-Oriented Software.

92

31/0

3/03

Factory Pattern

Color

+print()

Print()

Brown

+ print()

Red

+ print()

UNKNOWN_EXIT_STATE

+ print()

ColorFactory

+ create()

Client

Black

+ print()

93

31/0

3/03

Factory Pattern

The switch is now hidden in the object state factory.

The factory returns a new object of the right derived class by using a switch case on the state integer code.

The client always sees the root class object type Color.

Each state object know how to manage client invocation in its case.

94

31/0

3/03

Factory pseudo code

static color create(int Color) {

switch(Color) {

case Black : return color = new black();break;

case Brown:return color = new brown();break;

case Red:return color = new red();break;

case Orange : return color = new Orange();break;

case Yellow: return color = new Yellow();break;

case Green : return color = new Green();break;

case Blue : return color = new Blue();

} }

New Object

95

31/0

3/03

1

31/0

3 /03

OOD in software industryDesign for changes

Sources of ChangeDesigning and programming in future tense

Procedural Oriented Design (POD) VS Object Oriented Design (OOD)Procedural Oriented Design

Design The Problem StatementConsequence of Problem Statement changeState and Procedure Dichotomy

Object Oriented DesignIntegrate State and ProcedureDesign A Solution StatementNo Consequence of Problem Statement change

GOF Design PatternFactoryState pattern

96

31/0

3/03

Dynamic Polymorphism

Static polymorphism (Factory)State machine

Pattern State creates one object for each state and uses polymorphism to enable transparent client invocation.

Dynamic Polymorphism

97

31/0

3/03

State Pattern (from the GoF)

GoF stand for Gang of Four. It refers to the famous books of Vlisside and Co. Design Patterns: Elements of Reusable Object-Oriented Software.

98

31/0

3/03

State model transformation

ColorRequest()

Client

99

31/0

3/03

State diagram

Brown

Request()

Black OrangeRequest()

Request()

Request()Red

100

31/0

3/03

structural switch

static void printsState(int state) {

switch(state) {

case Black :

System.out.println( “Black"); break;

case Brown :

System.out.println( “Brown"); break;

case Red :

System.out.println( “Red"); break;

case Orange :

System.out.println( “Orange"); break;

} }

101

31/0

3/03

structural switch

static void printsState(int state) {

switch(state) {

case Black :

System.out.println( “Black"); break;

case Brown :

System.out.println( “Brown"); break;

case Red :

System.out.println( “Red"); break;

case Orange :

System.out.println( “Orange"); break;

} }

Spaghetti Plate

102

31/0

3/03

State model transformation

ColorRequest()

Client

103

31/0

3/03

State model transformation

ContextRequest()

Client

104

31/0

3/03

State model transformation

HelloContextRequest()

OrangeHandle()

ColorHandle()

BrownHandle()

Client

BlackHandle()

105

31/0

3/03

State model transformation

HelloContextRequest()

OrangeHandle()

BlackHandle()

ColorHandle()

BrownHandle()

Context

Concrete state

Client

106

31/0

3/03

Polymorphism and state patterns

If we have an object which state can change during its lifetime and we have to perform different operations according to the object state we use the pattern state.

pattern State avoids switch even if the state of the object is changing

pattern State creates one object for each state and uses polymorphism to enable transparent client invocation.

107

31/0

3/03

Objective

State pattern avoid structural switchNo enumeration in Java

An enumeration may be managed as a state machine.

108

31/0

3/03

Next step

Model Driven DevelopmentCode generation

109

31/0

3/03

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

Object Oriented Design (OOD)in software industry

The second order polynomial example

Emmanuel FUCHS

111

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

112

31/0

3/03

Second order polynomial equation

02 =++ cbxax

113

31/0

3/03

Second order polynomial equation roots:

aacbbx

242 −±−

=

114

31/0

3/03

Second order polynomial discriminant :

acb 42 −=∆

115

31/0

3/03

X1 X2

y

First iteration : find roots in R

x

116

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

117

31/0

3/03

Changes Request : flexible solution

Initial Request Evolution Request

1 Day After

1 Week After

Few Weeks Later

1 Day After

118

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

119

31/0

3/03

Second iteration : find roots in C

X1 X2

j

x

120

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

121

31/0

3/03

Changes Request : flexible solution

Initial Request Evolution Request

1 Day After

1 Week After

Few Weeks Later

1 Day After

122

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

123

31/0

3/03

Third iteration : Checks Roots Interval

y

X1 X2

X

x

XX

124

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

125

31/0

3/03

Changes Request : flexible solution

Initial Request Evolution Request

1 Day After

1 Week After

Few Weeks Later ?

1 Day After

126

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

127

31/0

3/03

Procedural solutionStart

Input coefficients a,b,c

Computes discriminantDelta = b * b – 4 * a * c

Computes single root

= 0

< 0> 0 DiscriminantSign

Computes roots

print roots print root print no roots

End

128

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

129

31/0

3/03

Source code

delta = (b*b) - (4*a*c); // discrimant computation

if (delta < 0.0) { System.out.println (" No roots");

}

else if (delta > 0.0) { System.out.println (" Two roots :"); System.out.println (" x1 = " + (-b + Math.sqrt(delta))/ (2.0 * a)); System.out.println (" x2 = " + (-b - Math.sqrt(delta))/ (2.0 * a));

}

else { System.out.println (“ Single root: "); System.out.println (" x = " + (-b / (2.0 * a)));

}

130

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

131

31/0

3/03

Procedural solutionStart

Input coefficients a,b,c

Computes discriminantDelta = b * b – 4 * a * c

Computes single root

= 0

< 0> 0 DiscriminantSign

Computes roots

print roots print root print no roots

End

132

31/0

3/03

Procedural solution Complex roots

Start

Computes discriminantDelta = b * b – 4 * a * c

DiscriminantSign

Computes double rootComputes roots

print roots print root print roots

Input coefficients a,b,c

End

< 0

= 0

> 0

Computes Complex roots

133

31/0

3/03

Source code

delta = (b*b) - (4*a*c); // discrimant computation

if (delta < 0.0) { System.out.println (" No roots");

}

else if (delta > 0.0) { System.out.println (" Two roots :"); System.out.println (" x1 = " + (-b + Math.sqrt(delta))/ (2.0 * a)); System.out.println (" x2 = " + (-b - Math.sqrt(delta))/ (2.0 * a));

}

else { System.out.println (“ Single root: "); System.out.println (" x = " + (-b / (2.0 * a)));

}

134

31/0

3/03

Source code first modification

delta = (b*b) - (4*a*c); // discrimant computation

if (delta < 0.0) { System.out.println (" No roots");

}

else if (delta > 0.0) { System.out.println (" Two roots :"); System.out.println (" x1 = " + (-b + Math.sqrt(delta))/ (2.0 * a)); System.out.println (" x2 = " + (-b - Math.sqrt(delta))/ (2.0 * a));

System.out.println (" Complex roots"); System.out.println (" x1 real part = " + (-b / (2.0*a))); System.out.println (" x2 imaginary part = " + (-b - Math.sqrt(-delta))/ (2.0*a)+ "i"); System.out.println (" x2 real part = " + (-b / (2.0*a))); System.out.println (" x2 imaginary part= " + (-b - Math.sqrt(-delta))/ (2.0*a)+ "i");

}

else { System.out.println (“ Single root: "); System.out.println (" x = " + (-b / (2.0 * a)));

}

135

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

136

31/0

3/03

Functional evolution : Checks Roots Interval

y

X1 X2

X

x

XX

137

31/0

3/03

Checks Roots Interval : procedural solution

End

< 0> 0

( /X1/ <= X & X <= /X2/ )

Return True Return False

F FT

DiscriminatSign

= 0Computes root case 1 Computes roots case 2 2

TX == X1

Return True Return False Return False

138

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

139

31/0

3/03

Changes Request : flexible solution

Initial Request Evolution Request

1 Day After

1 Week After

Few Weeks Later ?

1 Day After

140

31/0

3/03

Source Code

static boolean isInBetweenRoots(double x,double a,double b, double c) {

double delta, x1, x2; delta = (b*b) - (4*a*c);

if (delta < 0.0)

return false;

else if (delta > 0.0) {

System.out.print("delta > 0"); x1 = (-b + Math.sqrt(delta))/ (2.0*a); x2 = (-b - Math.sqrt(delta))/ (2.0*a); return (Math.abs(x1) <= Math.abs(x)) && (Math.abs(x) <= Math.abs(x2));

}

else { x1 = -b / (2.0 * a); return (x == x1);

} }

141

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

142

31/0

3/03

First Object Model : Domain Model

Second OrderPolynomial

Single RootSecond Order

Polynomial

Two RootsSecond Order

Polynomial

No RootSecond Order

Polynomial

143

31/0

3/03

First Object Model : Domain Model

Second OrderPolynomial Discriminant

Single RootSecond Order

Polynomial

Two RootsSecond Order

Polynomial

No RootSecond Order

Polynomial

144

31/0

3/03

First Design Model with operation

Second OrderPolynomial

computeRoots()

Double aDouble bDouble c

Discriminant

Single RootSecond Order

Polynomial

computeRoots()

Two RootsSecond Order

Polynomial

computeRoots()

No RootSecond Order

Polynomial

computeRoots()

145

31/0

3/03

Design Model Creation without Factory

Second OrderPolynomial

computeRoots()create()

Single RootSecond Order

PolynomialcomputeRoots()

Two RootsSecond Order

PolynomialcomputeRoots()

No RootSecond Order

PolynomialcomputeRoots()

146

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

147

31/0

3/03

Domain Model

Second OrderPolynomial

Discriminant

148

31/0

3/03

Domain Model: Polynomial FactorySecond Order

Polynomial

Second OrderPolynomial

Factory

Discriminant

create

149

31/0

3/03

Domain Model: Polynomial FactorySecond Order

Polynomial

Second OrderPolynomial

Factory

Discriminant

150

31/0

3/03

First Design Model with operation

Second OrderPolynomial

computeRoots()

Double aDouble bDouble c

Discriminant

Single RootSecond Order

Polynomial

computeRoots()

Two RootsSecond Order

Polynomial

computeRoots()

No RootSecond Order

Polynomial

computeRoots()

151

31/0

3/03

Discriminant

class Discriminant {

private double delta;

public Discriminant (double a, double b, double c) {

delta = (b * b) - (4.0 * a * c);

}

public double value () {

return delta; }

}

152

31/0

3/03

Factory: Initial Requirements

static Polynome create( double a, double b, double c) {

Discriminant theDiscriminant = new Discriminant(a,b,c); double delta = theDiscriminant.value();Polynome polynome;

if (delta == 0.0) {

return polynome = new SingleRootPolynome(a,b,c,theDiscriminant) ; } else if (delta > 0.0) {

return polynome = new TwoRootsPolynome(a,b,c,theDiscriminant) ; } else {

return polynome = new NoRootPolynome(a,b,c,theDiscriminant); }

}

153

31/0

3/03

Factory 1° Requirements Change

static Polynome create( double a, double b, double c) {

Discriminant theDiscriminant = new Discriminant(a,b,c); double delta = theDiscriminant.value(); Polynome polynome;

if (delta == 0.0) {

return polynome = new SingleRootPolynome(a,b,c,theDiscriminant) ; } else if (delta > 0.0) {

return polynome = new TwoRootsPolynome(a,b,c,theDiscriminant) ; } else {

return polynome = new ComplexRootsPolynome(a,b,c,theDiscriminant); }

}

154

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

155

31/0

3/03

Design Model with operation

Second OrderPolynomial

computeRoots()

Double aDouble bDouble c

Discriminant

Single RootSecond Order

Polynomial

computeRoots()

Two RootsSecond Order

Polynomial

computeRoots()

No RootSecond Order

Polynomial

computeRoots()

156

31/0

3/03

Computes root : Single Root Polynomial

void computesRoots() {

System.out.println (" Single root: ");

System.out.println (" x = " + (-b / (2.0*a)));

}

157

31/0

3/03

Design Model with operation

Second OrderPolynomial

computeRoots()

Double aDouble bDouble c

Discriminant

Single RootSecond Order

Polynomial

computeRoots()

Two RootsSecond Order

Polynomial

computeRoots()

No RootSecond Order

Polynomial

computeRoots()

158

31/0

3/03

Computes root : Two Roots Polynomial

void computesRoots() {

System.out.println (" Two roots :");

System.out.println (" x1 = " + (-b + Math.sqrt(discriminant.value()))/ (2.0*a));

System.out.println (" x2 = " + (-b - Math.sqrt(discriminant.value()))/ (2.0*a));

}

159

31/0

3/03

Design Model with operation

Second OrderPolynomial

computeRoots()

Double aDouble bDouble c

Discriminant

Single RootSecond Order

Polynomial

computeRoots()

Two RootsSecond Order

Polynomial

computeRoots()

No RootSecond Order

Polynomial

computeRoots()

160

31/0

3/03

Computes root : No Root Polynomial

void computesRoots() {

System.out.println (" No roots");

}

161

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

162

31/0

3/03

Design Model with operation

Second OrderPolynomial

computeRoots()

Double aDouble bDouble c

Discriminant

Single RootSecond Order

Polynomial

computeRoots()

Two RootsSecond Order

Polynomial

computeRoots()

No RootSecond Order

Polynomial

computeRoots()

163

31/0

3/03

Design Model with operation

Second OrderPolynomial

computeRoots()

Double aDouble bDouble c

Discriminant

Single RootSecond Order

Polynomial

computeRoots()

Two RootsSecond Order

Polynomial

computeRoots()

Complex RootsSecond Order

Polynomial

computeRoots()

164

31/0

3/03

Computes root : Complex Roots Polynomial

void computesRoots() {

System.out.println (" Complex roots");

System.out.println (" x1 real part = " + (-b / (2.0*a)));

System.out.println (" x1 imaginary part = “

+ (-b + Math.sqrt(-discriminant.value()))/ (2.0*a)+ "i");

System.out.println (" x2 real part = " + (-b / (2.0*a)));

System.out.println (" x2 imaginary part = “

+ (-b - Math.sqrt(-discriminant.value()))/ (2.0*a)+ "i");

}

165

31/0

3/03

1

Second order polynomial

The problem1° requirements change2° requirements changeProcedural solutionInitial requirements1° requirements change2° requirements changeObject SolutionPolynomial FactoryInitial requirements1° requirements change2° requirements change

166

31/0

3/03

Design Model

Second OrderPolynomial

computeRoots()isInBetweenRoots()

Single RootSecond Order

PolynomialcomputeRoots()

isInBetweenRoots()

Two RootsSecond Order

PolynomialcomputeRoots()

isInBetweenRoots()

No RootSecond Order

PolynomialcomputeRoots()

isInBetweenRoots()

167

31/0

3/03

Bracket the parameter : single root

boolean isInBetweenRoots(double x) {

return (x == x1);

}

168

31/0

3/03

Design Model

Second OrderPolynomial

computeRoots()isInBetweenRoots()

Single RootSecond Order

PolynomialcomputeRoots()

isInBetweenRoots()

Two RootsSecond Order

PolynomialcomputeRoots()

isInBetweenRoots()

No RootSecond Order

PolynomialcomputeRoots()

isInBetweenRoots()

169

31/0

3/03

Bracket the parameter : Two roots

boolean isInBetweenRoots(double x) {

return (Math.abs(x1) <= Math.abs(x)) && (Math.abs(x) <= Math.abs(x2));

}

170

31/0

3/03

Design Model

Second OrderPolynomial

computeRoots()isInBetweenRoots()

Single RootSecond Order

PolynomialcomputeRoots()

isInBetweenRoots()

Two RootsSecond Order

PolynomialcomputeRoots()

isInBetweenRoots()

No RootSecond Order

PolynomialcomputeRoots()

isInBetweenRoots()

171

31/0

3/03

Bracket the parameter : no roots

boolean isInBetweenRoots(double x) {

return false;

}

172

31/0

3/03

That’s it !!!

173

31/0

3/03

Thank You For Your Attention

Questions are welcomeContacts :

emmanuel.fuchs@elfuchs.com

www.elfuchs.com