SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin
Agenda – Lecture 11b DiceGame New GoF patterns. Exercise Set. Summary of redesign done so far. Relationship to patterns and principles. New functionality. New GoF patterns. Exercise Set. 1/12/2019 SOEN 343, © P.Chalin,
What were the patterns we saw … In our DiceGame redesign … 1/12/2019 SOEN 343, © P.Chalin,
Dice Game Refactoring We applied: Indirection principle. Decoupled DiceGame from Dice by inserting an IDice interface in between. Introduced a Simple Factory class, DiceFactory. Made DiceFactory a Singleton. 1/12/2019 SOEN 343, © P.Chalin,
Dice Game: New Functionality Although we added ConstDice we still have a Problem: Our DiceFactory only creates one type of IDice. Solutions: Somehow we want the behavior of createDice() to vary. 1/12/2019 SOEN 343, © P.Chalin,
DiceFactory.createDice(). Possible solutions: DiceFactory can read a System property to determine the class to instantiate (Larman05, 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. 1/12/2019 SOEN 343, © P.Chalin,
DiceGameTest JUnit TestCase public void testWinning() { int faceValue = 3; DiceFactory.theOne(). DiceGame game = new DiceGame(); game.roll(); assertEquals("face value", faceValue, game.getFaceValue()); assertTrue("won", game.won()); } 1/12/2019 SOEN 343, © P.Chalin,
New Version of Dice Game Support using a pair of Dice. How can this be added? 1/12/2019 SOEN 343, © P.Chalin,
Multi-Dice Design 1/12/2019 SOEN 343, © P.Chalin,
Composite (Larman05, 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. 1/12/2019 SOEN 343, © P.Chalin,
Dice Composite 1/12/2019 SOEN 343, © P.Chalin,
Client Cannot Tell … SOEN 343, © P.Chalin, DiceGame cannot tell whether it is dealing with one or more dice. 1/12/2019 SOEN 343, © P.Chalin,
Composite: Ex. Objects Credits: GoF. 1/12/2019 SOEN 343, © P.Chalin,
Composite: Ex. Class Diagram 1/12/2019 SOEN 343, © P.Chalin,
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 UnsupportedOperationException(); } Can you think of a better solution? 1/12/2019 SOEN 343, © P.Chalin,
Composite: Clients point-of-view 1/12/2019 SOEN 343, © P.Chalin,
Composite Pricing Strategies Interface Realization 1/12/2019 SOEN 343, © P.Chalin,
Patterns and Principles We have (and still are) studying: Larman’s GRASP GoF Fowler’s EA The most fundamental are the principles. 1/12/2019 SOEN 343, © P.Chalin,
GRASP: Interrelationships This is how Larman illustrates the interrelationships between the GRASP. 1/12/2019 SOEN 343, © P.Chalin,
Patterns apply principles, e.g. … This is how Larman illustrates the interrelationships between the GRASP. 1/12/2019 SOEN 343, © P.Chalin,
Gang-of-four … a closer look 1/12/2019 SOEN 343, © P.Chalin,
Gang Of Four Gamma, Helm, Johnson, Vlissides Some patterns covered in Larman, Chap. 23,… All patterns in XDE As documentation. As dynamic templates. Erich 1/12/2019 SOEN 343, © P.Chalin,
GoF Pattern Summary (& Relationhips) [Picutre (c) GoF CD] 1/12/2019 SOEN 343, © P.Chalin,
GoF Pattern Classification Behavioral Patterns Creational Patterns Structural Patterns 1/12/2019 SOEN 343, © P.Chalin,
GoF Behavioral Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor 1/12/2019 SOEN 343, © P.Chalin,
GoF Creational Patterns Abstract Factory Builder Factory Method (we saw Simple Factory) Prototype Singleton 1/12/2019 SOEN 343, © P.Chalin,
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. 1/12/2019 SOEN 343, © P.Chalin,
Factory Example SOEN 343, © P.Chalin, Have we seen code like this anywhere? 1/12/2019 SOEN 343, © P.Chalin,
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 1/12/2019 SOEN 343, © P.Chalin,
(Abstract) Factory Example (GoF) 1/12/2019 SOEN 343, © P.Chalin,
FrontControllerServlet RemoveStudentCommand Factory (in EAs) FrontControllerServlet FrontCommand # processRequest ( ) + init ( ) - getCommand ( ) : FrontCommand + processRequest ( ) - getCommandClass ( ) RemoveStudentCommand ViewStudInfoCommand + processRequest ( ) + processRequest ( ) 1/12/2019 SOEN 343, © P.Chalin,
GoF Structural Patterns Adapter Bridge Composite Decorator Facade Flyweight Proxy 1/12/2019 SOEN 343, © P.Chalin,
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. 1/12/2019 SOEN 343, © P.Chalin,
Adapter Suppose we have a tax calculation class (or external library) but the interface is not well suited for our application. 1/12/2019 SOEN 343, © P.Chalin,
Adapter Adapter provides an interface suited to the application GoodAsGoldTaxProAdapter getTaxes( Sale ) : List of TaxLineItems computeTax(…):double GoodAsGoldTaxPro Adapter provides an interface suited to the application 1/12/2019 SOEN 343, © P.Chalin,
Adapter (For More than One Class) What if more than one class (library) needs to be adapted? 1/12/2019 SOEN 343, © P.Chalin,
Adapter 1/12/2019 SOEN 343, © P.Chalin,