SOEN 343 Software Design Computer Science and Software

  • Slides: 37
Download presentation
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005

SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin SOEN 343, © P. Chalin SOEN 344, © P. Chalin

Agenda – Lecture 11 b • Dice. Game – Summary of redesign done so

Agenda – Lecture 11 b • Dice. Game – Summary of redesign done so far. – Relationship to patterns and principles. – New functionality. • New Go. F patterns. • Exercise Set. 9/10/2020 SOEN 343, © P. Chalin, 2

What were the patterns we saw … • In our Dice. Game redesign …

What were the patterns we saw … • In our Dice. Game redesign … 9/10/2020 SOEN 343, © P. Chalin, 3

Dice Game Refactoring We applied: • Indirection principle. – Decoupled Dice. Game from Dice

Dice Game Refactoring We applied: • Indirection principle. – Decoupled Dice. Game from Dice by inserting an IDice interface in between. • Introduced a Simple Factory class, Dice. Factory. • Made Dice. Factory a Singleton. 9/10/2020 SOEN 343, © P. Chalin, 4

Dice Game: New Functionality Although we added Const. Dice we still have a Problem:

Dice Game: New Functionality Although we added Const. Dice we still have a Problem: • Our Dice. Factory only creates one type of IDice. Solutions: • Somehow we want the behavior of create. Dice() to vary. 9/10/2020 SOEN 343, © P. Chalin, 5

Dice. Factory. create. Dice(). • Possible solutions: – Dice. Factory can read a System

Dice. Factory. create. Dice(). • Possible solutions: – Dice. Factory can read a System property to determine the class to instantiate (Larman 05, p. 441). – Add a method to factory: – Generalize our solution further: Abstract Factory. • Any of these solutions will finally allow us to create our test class, … e. g. 9/10/2020 SOEN 343, © P. Chalin, 6

Dice. Game. Test JUnit Test. Case public void test. Winning() { int face. Value

Dice. Game. Test JUnit Test. Case public void test. Winning() { int face. Value = 3; Dice. Factory. the. One(). Dice. Game game = new Dice. Game(); game. roll(); assert. Equals("face value", face. Value, game. get. Face. Value()); assert. True("won", game. won()); 9/10/2020 SOEN 343, © P. Chalin, 7

New Version of Dice Game • Support using a pair of Dice. • How

New Version of Dice Game • Support using a pair of Dice. • How can this be added? 9/10/2020 SOEN 343, © P. Chalin, 8

Multi-Dice Design 9/10/2020 SOEN 343, © P. Chalin, 9

Multi-Dice Design 9/10/2020 SOEN 343, © P. Chalin, 9

Composite (Larman 05, 26. 8) Context/problem • How do you treat a group or

Composite (Larman 05, 26. 8) Context/problem • How do you treat a group or composite structure of objects the same way (polymorphically) as a non-composite (atomic) object? Solution • Define classes for composite and atomic objects so that they implement the same interface. 9/10/2020 SOEN 343, © P. Chalin, 10

Dice Composite 9/10/2020 SOEN 343, © P. Chalin, 11

Dice Composite 9/10/2020 SOEN 343, © P. Chalin, 11

Client Cannot Tell … 9/10/2020 SOEN 343, © P. Chalin, 12

Client Cannot Tell … 9/10/2020 SOEN 343, © P. Chalin, 12

Composite: Ex. Objects 9/10/2020 SOEN 343, © P. Chalin, 13

Composite: Ex. Objects 9/10/2020 SOEN 343, © P. Chalin, 13

Composite: Ex. Class Diagram 9/10/2020 SOEN 343, © P. Chalin, 14

Composite: Ex. Class Diagram 9/10/2020 SOEN 343, © P. Chalin, 14

Must “add” be implemented by Line? • In C++ add declared virtual; subclass need

Must “add” be implemented by Line? • In C++ add declared virtual; subclass need not implement it. • In Java if add is abstract, then subclasses must implement it. String add(Graphic g) { throw new Unsupported. Operation. Exception(); } • Can you think of a better solution? 9/10/2020 SOEN 343, © P. Chalin, 15

Composite: Clients point-of-view 9/10/2020 SOEN 343, © P. Chalin, 16

Composite: Clients point-of-view 9/10/2020 SOEN 343, © P. Chalin, 16

Composite Pricing Strategies • Interface • Realization 9/10/2020 SOEN 343, © P. Chalin, 17

Composite Pricing Strategies • Interface • Realization 9/10/2020 SOEN 343, © P. Chalin, 17

Patterns and Principles We have (and still are) studying: • Larman’s GRASP • Go.

Patterns and Principles We have (and still are) studying: • Larman’s GRASP • Go. F • Fowler’s EA The most fundamental are the principles. 9/10/2020 SOEN 343, © P. Chalin, 18

GRASP: Interrelationships 9/10/2020 SOEN 343, © P. Chalin, 19

GRASP: Interrelationships 9/10/2020 SOEN 343, © P. Chalin, 19

Patterns apply principles, e. g. … 9/10/2020 SOEN 343, © P. Chalin, 20

Patterns apply principles, e. g. … 9/10/2020 SOEN 343, © P. Chalin, 20

Gang-of-four … a closer look 9/10/2020 SOEN 343, © P. Chalin, 21

Gang-of-four … a closer look 9/10/2020 SOEN 343, © P. Chalin, 21

Gang Of Four • Gamma, Helm, Johnson, Vlissides • Some patterns covered in Larman,

Gang Of Four • Gamma, Helm, Johnson, Vlissides • Some patterns covered in Larman, Chap. 23, … • All patterns in XDE – As documentation. – As dynamic templates. 9/10/2020 SOEN 343, © P. Chalin, 22

Go. F Pattern Summary (& Relationhips) 9/10/2020 SOEN 343, © P. Chalin, 23

Go. F Pattern Summary (& Relationhips) 9/10/2020 SOEN 343, © P. Chalin, 23

Go. F Pattern Classification • Behavioral Patterns • Creational Patterns • Structural Patterns 9/10/2020

Go. F Pattern Classification • Behavioral Patterns • Creational Patterns • Structural Patterns 9/10/2020 SOEN 343, © P. Chalin, 24

Go. F Behavioral Patterns • Chain of Responsibility • Command • Interpreter • Iterator

Go. F Behavioral Patterns • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento 9/10/2020 • • • Observer State Strategy Template Method Visitor SOEN 343, © P. Chalin, 25

Go. F Creational Patterns • • • Abstract Factory Builder Factory Method (we saw

Go. F Creational Patterns • • • Abstract Factory Builder Factory Method (we saw Simple Factory) Prototype Singleton 9/10/2020 SOEN 343, © P. Chalin, 26

Factory • Context / problem: Who should be responsible for creating objects when there

Factory • Context / problem: Who should be responsible for creating objects when there are special considerations, such as complex creation logic, a desire to separate the creation responsibilities for better cohesion, and so forth? • Solution: Create a Pure Fabrication object called a Factory. 9/10/2020 SOEN 343, © P. Chalin, 27

Factory Example 9/10/2020 SOEN 343, © P. Chalin, 28

Factory Example 9/10/2020 SOEN 343, © P. Chalin, 28

Larman’s comment on prev. figure • Note that the factory methods return objects types

Larman’s comment on prev. figure • Note that the factory methods return objects types to an interfacre rather than a class so that the factory can return any implementation of the interface. • Factory methods can also 9/10/2020 SOEN 343, © P. Chalin, 29

(Abstract) Factory Example (Go. F) 9/10/2020 SOEN 343, © P. Chalin, 30

(Abstract) Factory Example (Go. F) 9/10/2020 SOEN 343, © P. Chalin, 30

Factory (in EAs) Front. Command Front. Controller. Servlet # process. Request ( ) -

Factory (in EAs) Front. Command Front. Controller. Servlet # process. Request ( ) - get. Command ( ) : Front. Command - get. Command. Class ( ) + init ( ) + process. Request ( ) Remove. Student. Command + process. Request ( ) 9/10/2020 View. Stud. Info. Command + process. Request ( ) SOEN 343, © P. Chalin, 31

Go. F Structural Patterns • • Adapter Bridge Composite Decorator Facade Flyweight Proxy 9/10/2020

Go. F Structural Patterns • • Adapter Bridge Composite Decorator Facade Flyweight Proxy 9/10/2020 SOEN 343, © P. Chalin, 32

Adapter • Context / problem How to resolve incompatible interfaces, or provide a stable

Adapter • Context / problem How to resolve incompatible interfaces, or provide a stable interface to similar components with different interfaces? • Solution: Convert the original interface of a component into another interface, through an intermediate adapter object. 9/10/2020 SOEN 343, © P. Chalin, 33

Adapter • Suppose we have a tax calculation class (or external library) but the

Adapter • Suppose we have a tax calculation class (or external library) but the interface is not well suited for our application. 9/10/2020 SOEN 343, © P. Chalin, 34

Adapter • Adapter provides an interface suited to the application Good. As. Gold. Tax.

Adapter • Adapter provides an interface suited to the application Good. As. Gold. Tax. Pro. Adapter get. Taxes( Sale ) : List of Tax. Line. Items Good. As. Gold. Tax. Pro compute. Tax(…): double 9/10/2020 SOEN 343, © P. Chalin, 35

Adapter (For More than One Class) • What if more than one class (library)

Adapter (For More than One Class) • What if more than one class (library) needs to be adapted? 9/10/2020 SOEN 343, © P. Chalin, 36

Adapter 9/10/2020 SOEN 343, © P. Chalin, 37

Adapter 9/10/2020 SOEN 343, © P. Chalin, 37