173
Object Oriented Design (OOD) in software industry Available at www.elfuchs.com Emmanuel FUCHS

Object Oriented Design And Programing

Embed Size (px)

DESCRIPTION

Polymorphism Vs Enumerated

Citation preview

Page 1: Object Oriented Design And Programing

Object Oriented Design (OOD)in software industry

Available at www.elfuchs.com

Emmanuel FUCHS

Page 2: Object Oriented Design And Programing

2

31/0

3/03

Why OOD ?

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

Page 3: Object Oriented Design And Programing

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

Page 4: Object Oriented Design And Programing

4

31/0

3/03

Changes Request : flexible solution

Productive programmerInitial Request

1 Day After

Unproductive programmer

1 Week After

Page 5: Object Oriented Design And Programing

5

31/0

3/03

Changes Request : flexible solution

Productive programmerEvolution

RequestInitial Request

1 Day After

Unproductive programmer

1 Week After

Page 6: Object Oriented Design And Programing

6

31/0

3/03

Changes Request : flexible solution

Productive programmerEvolution

RequestInitial Request

1 Day After

1 Week After

Unproductive programmer

1 Day After

Page 7: Object Oriented Design And Programing

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

Page 8: Object Oriented Design And Programing

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

Page 9: Object Oriented Design And Programing

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

Page 10: Object Oriented Design And Programing

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

Page 11: Object Oriented Design And Programing

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

Page 12: Object Oriented Design And Programing

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

Page 13: Object Oriented Design And Programing

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

Page 14: Object Oriented Design And Programing

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

Page 15: Object Oriented Design And Programing

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

Page 16: Object Oriented Design And Programing

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.

Page 17: Object Oriented Design And Programing

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

Page 18: Object Oriented Design And Programing

18

31/0

3/03

User Input

Operation:

A: Addition

B: Subtraction

C: Division

E: Multiplication

Enter a choice =>

Page 19: Object Oriented Design And Programing

19

31/0

3/03

User Input

Operation: Addition

A: First Integer

B: Second Integer

Enter a choice =>

Page 20: Object Oriented Design And Programing

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

Page 21: Object Oriented Design And Programing

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

Page 22: Object Oriented Design And Programing

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

Page 23: Object Oriented Design And Programing

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

Page 24: Object Oriented Design And Programing

24

31/0

3/03

User Input : Operation in R and C

Operation:

A: Real

B: Complex

Enter a choice =>

Page 25: Object Oriented Design And Programing

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 =>

Page 26: Object Oriented Design And Programing

26

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

Page 27: Object Oriented Design And Programing

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

Page 28: Object Oriented Design And Programing

28

31/0

3/03

Hierarchical, Structural Programming, “Top Down”

Reuse of Real operations for Complex Operations

Procedures

Page 29: Object Oriented Design And Programing

29

31/0

3/03

Hierarchical, Structural Programming, “Top Down”

Reuse of Real operations for Complex Operations

Procedures

Page 30: Object Oriented Design And Programing

30

31/0

3/03

Hierarchical, Structural Programming, “Top Down”

Reuse of Real operations for Complex Operations

Procedures

Spaghetti Plate

Page 31: Object Oriented Design And Programing

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

Page 32: Object Oriented Design And Programing

32

31/0

3/03

State and Procedure dichotomy

State Procedures

Page 33: Object Oriented Design And Programing

33

31/0

3/03

State and Procedure dichotomy

State Procedures

Page 34: Object Oriented Design And Programing

34

31/0

3/03

State and Procedure dichotomy

State Procedures

Spaghetti Plate

Page 35: Object Oriented Design And Programing

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

Page 36: Object Oriented Design And Programing

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.

Page 37: Object Oriented Design And Programing

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.

Page 38: Object Oriented Design And Programing

38

31/0

3/03

Object analogy

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

Implementation Interface

Page 39: Object Oriented Design And Programing

39

31/0

3/03

Object analogy

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

Implementation Interface

Page 40: Object Oriented Design And Programing

40

31/0

3/03

Object analogy

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

Implementation Interface

Page 41: Object Oriented Design And Programing

41

31/0

3/03

Polymorphism

Interface

(specification) Implementation

(body)

Page 42: Object Oriented Design And Programing

42

31/0

3/03

Object Interface

UML class

Name

Attribute

OperationOperationOperationOperation

Object Interface

Client

Page 43: Object Oriented Design And Programing

43

31/0

3/03

Object Interface and Implementation

Name

Attribute

OperationOperationOperationOperation

UML class

Object Interface

Client

Interface Implementation

Page 44: Object Oriented Design And Programing

44

31/0

3/03

Object Interface and Implementation

Name

Attribute

OperationOperationOperationOperation

UML class

Object Interface

Client

Interface Implementation

Page 45: Object Oriented Design And Programing

45

31/0

3/03

Object collaboration

Page 46: Object Oriented Design And Programing

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

Page 47: Object Oriented Design And Programing

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

Page 48: Object Oriented Design And Programing

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 =>

Page 49: Object Oriented Design And Programing

49

31/0

3/03

Replace case and enum by object .

enum Color {

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

}

Page 50: Object Oriented Design And Programing

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

} }

Page 51: Object Oriented Design And Programing

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

Page 52: Object Oriented Design And Programing

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.

Page 53: Object Oriented Design And Programing

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 =>

Page 54: Object Oriented Design And Programing

54

31/0

3/03

Replace case and enum by object .

enum Color {

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

}

Page 55: Object Oriented Design And Programing

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

} }

Page 56: Object Oriented Design And Programing

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

Page 57: Object Oriented Design And Programing

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

Page 58: Object Oriented Design And Programing

58

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

Page 59: Object Oriented Design And Programing

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

Page 60: Object Oriented Design And Programing

60

31/0

3/03

The object solution

Polymorphism

Page 61: Object Oriented Design And Programing

61

31/0

3/03

Polymorphism

Print()Color

+print()

Client

Page 62: Object Oriented Design And Programing

62

31/0

3/03

Polymorphism

Color

+print()

No Changes

Print()Client

Black

+ print()

Page 63: Object Oriented Design And Programing

63

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

No Changes

Print()Client

Black

+ print()

Page 64: Object Oriented Design And Programing

64

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

No Changes

Print()Client

Black

+ print()

Blue

+ print()

Page 65: Object Oriented Design And Programing

65

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

No Changes

Print()Client

Black

+ print()

Blue

+ print()

newColor

+ print()

Page 66: Object Oriented Design And Programing

66

31/0

3/03

Object Polymorphism

Code example

Page 67: Object Oriented Design And Programing

67

31/0

3/03

Polymorphism

Print()Color

+print()

Client

Page 68: Object Oriented Design And Programing

68

31/0

3/03

Polymorphism

Color

+print()

Print()Client

Black

+ print()

Page 69: Object Oriented Design And Programing

69

31/0

3/03

Code of the Black object print() operation

class Black extends Color

{

public void print()

{

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

}

Page 70: Object Oriented Design And Programing

70

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Print()Client

Black

+ print()

Page 71: Object Oriented Design And Programing

71

31/0

3/03

Code of the Brown object print() operation

class Brown extends Color

{

public void print()

{

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

}

Page 72: Object Oriented Design And Programing

72

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

Print()Client

Black

+ print()

Blue

+ print()

Page 73: Object Oriented Design And Programing

73

31/0

3/03

Code of the Red object print() operation

class Red extends Color

{

public void print()

{

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

}

Page 74: Object Oriented Design And Programing

74

31/0

3/03

Code of the Blue object print() operation

class Blue extends Color

{

public void print()

{

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

}

Page 75: Object Oriented Design And Programing

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

Page 76: Object Oriented Design And Programing

76

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

Print()Client

Black

+ print()

Blue

+ print()

Page 77: Object Oriented Design And Programing

77

31/0

3/03

Polymorphism

Color

+print()

Brown

+ print()

Red

+ print()

No Changes

Print()Client

Black

+ print()

Blue

+ print()

newColor

+ print()

Page 78: Object Oriented Design And Programing

78

31/0

3/03

Code of the Blue object print() operation

class NewColor extends Color

{

public void print()

{

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

}

Page 79: Object Oriented Design And Programing

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

Page 80: Object Oriented Design And Programing

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.

Page 81: Object Oriented Design And Programing

81

31/0

3/03

GOF: The book cover

Page 82: Object Oriented Design And Programing

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.

Page 83: Object Oriented Design And Programing

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.

Page 84: Object Oriented Design And Programing

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.

Page 85: Object Oriented Design And Programing

85

31/0

3/03

Design Pattern Catalogue

Creational PatternsAbstract Factory Builder Factory Method Prototype Singleton

Page 86: Object Oriented Design And Programing

86

31/0

3/03

Design Pattern Catalogue

Structural PatternsAdapter Bridge Composite Decorator Facade Flyweight Proxy

Page 87: Object Oriented Design And Programing

87

31/0

3/03

Design Pattern Catalogue

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

Page 88: Object Oriented Design And Programing

88

31/0

3/03

Design Pattern Map

Page 89: Object Oriented Design And Programing

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.

Page 90: Object Oriented Design And Programing

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

Page 91: Object Oriented Design And Programing

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.

Page 92: Object Oriented Design And Programing

92

31/0

3/03

Factory Pattern

Color

+print()

Print()

Brown

+ print()

Red

+ print()

UNKNOWN_EXIT_STATE

+ print()

ColorFactory

+ create()

Client

Black

+ print()

Page 93: Object Oriented Design And Programing

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.

Page 94: Object Oriented Design And Programing

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

Page 95: Object Oriented Design And Programing

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

Page 96: Object Oriented Design And Programing

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

Page 97: Object Oriented Design And Programing

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.

Page 98: Object Oriented Design And Programing

98

31/0

3/03

State model transformation

ColorRequest()

Client

Page 99: Object Oriented Design And Programing

99

31/0

3/03

State diagram

Brown

Request()

Black OrangeRequest()

Request()

Request()Red

Page 100: Object Oriented Design And Programing

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;

} }

Page 101: Object Oriented Design And Programing

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

Page 102: Object Oriented Design And Programing

102

31/0

3/03

State model transformation

ColorRequest()

Client

Page 103: Object Oriented Design And Programing

103

31/0

3/03

State model transformation

ContextRequest()

Client

Page 104: Object Oriented Design And Programing

104

31/0

3/03

State model transformation

HelloContextRequest()

OrangeHandle()

ColorHandle()

BrownHandle()

Client

BlackHandle()

Page 105: Object Oriented Design And Programing

105

31/0

3/03

State model transformation

HelloContextRequest()

OrangeHandle()

BlackHandle()

ColorHandle()

BrownHandle()

Context

Concrete state

Client

Page 106: Object Oriented Design And Programing

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.

Page 107: Object Oriented Design And Programing

107

31/0

3/03

Objective

State pattern avoid structural switchNo enumeration in Java

An enumeration may be managed as a state machine.

Page 108: Object Oriented Design And Programing

108

31/0

3/03

Next step

Model Driven DevelopmentCode generation

Page 109: Object Oriented Design And Programing

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

Page 110: Object Oriented Design And Programing

Object Oriented Design (OOD)in software industry

The second order polynomial example

Emmanuel FUCHS

Page 111: Object Oriented Design And Programing

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

Page 112: Object Oriented Design And Programing

112

31/0

3/03

Second order polynomial equation

02 =++ cbxax

Page 113: Object Oriented Design And Programing

113

31/0

3/03

Second order polynomial equation roots:

aacbbx

242 −±−

=

Page 114: Object Oriented Design And Programing

114

31/0

3/03

Second order polynomial discriminant :

acb 42 −=∆

Page 115: Object Oriented Design And Programing

115

31/0

3/03

X1 X2

y

First iteration : find roots in R

x

Page 116: Object Oriented Design And Programing

116

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

Page 117: Object Oriented Design And Programing

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

Page 118: Object Oriented Design And Programing

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

Page 119: Object Oriented Design And Programing

119

31/0

3/03

Second iteration : find roots in C

X1 X2

j

x

Page 120: Object Oriented Design And Programing

120

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

Page 121: Object Oriented Design And Programing

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

Page 122: Object Oriented Design And Programing

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

Page 123: Object Oriented Design And Programing

123

31/0

3/03

Third iteration : Checks Roots Interval

y

X1 X2

X

x

XX

Page 124: Object Oriented Design And Programing

124

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

Page 125: Object Oriented Design And Programing

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

Page 126: Object Oriented Design And Programing

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

Page 127: Object Oriented Design And Programing

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

Page 128: Object Oriented Design And Programing

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

Page 129: Object Oriented Design And Programing

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)));

}

Page 130: Object Oriented Design And Programing

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

Page 131: Object Oriented Design And Programing

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

Page 132: Object Oriented Design And Programing

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

Page 133: Object Oriented Design And Programing

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)));

}

Page 134: Object Oriented Design And Programing

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)));

}

Page 135: Object Oriented Design And Programing

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

Page 136: Object Oriented Design And Programing

136

31/0

3/03

Functional evolution : Checks Roots Interval

y

X1 X2

X

x

XX

Page 137: Object Oriented Design And Programing

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

Page 138: Object Oriented Design And Programing

138

31/0

3/03

Changes Request : flexible solution

Initial Request

1 Day After

1 Week After

Page 139: Object Oriented Design And Programing

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

Page 140: Object Oriented Design And Programing

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);

} }

Page 141: Object Oriented Design And Programing

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

Page 142: Object Oriented Design And Programing

142

31/0

3/03

First Object Model : Domain Model

Second OrderPolynomial

Single RootSecond Order

Polynomial

Two RootsSecond Order

Polynomial

No RootSecond Order

Polynomial

Page 143: Object Oriented Design And Programing

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

Page 144: Object Oriented Design And Programing

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()

Page 145: Object Oriented Design And Programing

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()

Page 146: Object Oriented Design And Programing

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

Page 147: Object Oriented Design And Programing

147

31/0

3/03

Domain Model

Second OrderPolynomial

Discriminant

Page 148: Object Oriented Design And Programing

148

31/0

3/03

Domain Model: Polynomial FactorySecond Order

Polynomial

Second OrderPolynomial

Factory

Discriminant

create

Page 149: Object Oriented Design And Programing

149

31/0

3/03

Domain Model: Polynomial FactorySecond Order

Polynomial

Second OrderPolynomial

Factory

Discriminant

Page 150: Object Oriented Design And Programing

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()

Page 151: Object Oriented Design And Programing

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; }

}

Page 152: Object Oriented Design And Programing

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); }

}

Page 153: Object Oriented Design And Programing

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); }

}

Page 154: Object Oriented Design And Programing

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

Page 155: Object Oriented Design And Programing

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()

Page 156: Object Oriented Design And Programing

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)));

}

Page 157: Object Oriented Design And Programing

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()

Page 158: Object Oriented Design And Programing

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));

}

Page 159: Object Oriented Design And Programing

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()

Page 160: Object Oriented Design And Programing

160

31/0

3/03

Computes root : No Root Polynomial

void computesRoots() {

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

}

Page 161: Object Oriented Design And Programing

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

Page 162: Object Oriented Design And Programing

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()

Page 163: Object Oriented Design And Programing

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()

Page 164: Object Oriented Design And Programing

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");

}

Page 165: Object Oriented Design And Programing

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

Page 166: Object Oriented Design And Programing

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()

Page 167: Object Oriented Design And Programing

167

31/0

3/03

Bracket the parameter : single root

boolean isInBetweenRoots(double x) {

return (x == x1);

}

Page 168: Object Oriented Design And Programing

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()

Page 169: Object Oriented Design And Programing

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));

}

Page 170: Object Oriented Design And Programing

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()

Page 171: Object Oriented Design And Programing

171

31/0

3/03

Bracket the parameter : no roots

boolean isInBetweenRoots(double x) {

return false;

}

Page 172: Object Oriented Design And Programing

172

31/0

3/03

That’s it !!!

Page 173: Object Oriented Design And Programing

173

31/0

3/03

Thank You For Your Attention

Questions are welcomeContacts :

[email protected]

www.elfuchs.com