Creational Design Patterns

Creational Design Patterns

F. Tip and M. Weintraub DESIGN PATTERNS Thanks go to Andreas Zeller for allowing incorporation of his materials WHERE THE SOFTWARE ENGINEERING TERM DESIGN PATTERNS COMES FROM Inspired by reusable elements of design (“patterns”) in the field of architecture Christopher Alexander, et al. A Pattern Language: Towns, Buildings, Construction, 1977 Presents 253 patterns, covering advice on use of materials, physical arrangements of architectural elements, etc. Examples 159. LIGHT ON TWO SIDES OF EVERY ROOM 173. GARDEN WALL 174. TRELLISED WALK 180. WINDOW PLACE 2 180. WINDOW PLACE Everybody loves window seats, bay windows, and big windows with low sills and comfortable chairs drawn up to them In every room where you spend any length of time during the day, make at least one window into a “window place” 3 ONE DIFFERENCE BETWEEN EXPERIENCED AND INEXPERIENCED SOFTWARE DESIGNERS Experienced designers know from experience what works and what doesn’t Often recognize “standard” design problems and apply “proven” solutions to them 4 PATTERNS IN SOFTWARE DESIGN The “Gang of Four” catalogued a number of widely used patterns in software design. GoF = Gamma, Helm, Johnson, Vlissides 5 REUSING EXPERIENCE: DESIGN PATTERNS Descriptions of communicating objects and classes adapted to solve a design problem in a specific context. 6 REUSING EXPERIENCE: DESIGN PATTERNS Descriptions of communicating objects and classes adapted to solve a design problem in a specific context. In other words: A design pattern is a generalized and reusable solution to a similar set of problems. Design patterns are abstract and must be tailored or adapted to each situation. 7 DESIGNING FOR CHANGE 1. Creating an object by specifying a class explicitly 2. Dependence on specific operations 3. Dependence on hardware/software platform Many design patterns introduce flexibility 4. Dependence on object representations or implementations to avoid common causes of redesign such as: 5. Algorithmic dependencies 6. Tight coupling 7. Extending functionality by sub-classing 8. Inability to alter classes conveniently 8 A DESIGN PATTERN HAS FOUR ELEMENTS 1. Name Key considerations: e.g, Abstract Factory or Visitor ▪ problem & solution have been observed in practice 2. Problem that the pattern addresses ▪ choice of implementation language is 3. Solution the program constructs significant that are part of the pattern 4. Consequences the results and tradeoffs of applying the pattern 9 CLASSIFYING DESIGN PATTERNS Purpose: what a pattern does Scope 1. Class-level 1. Creational concerned with relationship between concerned with creation of objects classes and their subclasses 2. Structural 2. Object-level related to composition of classes or concerned with object relationship objects (more dynamic, may be changed at run- 3. Behavioral time) related to interaction and distribution of responsibility 10 GOF DESIGN PATTERNS CLASSIFIED creational structural behavioral Interpreter class Factory Method Adapter (class) Template Method Chain of Resp. Adapter (object) Command Bridge Iterator Abstract Factory Composite Mediator Builder object Decorator Memento Prototype Façade Observer Singleton Flyweight State Proxy Strategy Visitor 11 CREATIONAL PATTERNS Purpose Abstract the process of creating objects Make a system unaware of how objects are created, composed, and represented What they do Encapsulate knowledge about which concrete classes a system uses ▪ access created objects via interfaces Hide how instances are created Provide flexibility for Types of created objects Responsibility for creation How and when objects are created 12 FIVE CLASSIC CREATIONAL PATTERNS 1. Abstract Factory 2. Builder 3. Factory Method 4. Prototype 5. Singleton 13 MOTIVATION: OVER TIME, CLASS MODELS GROW AND INSTANTIATION BECOMES CUMBERSOME. For example, suppose you have a that have the same construction. sandwich shop that offers a few types of sandwich 1. Prepare ingredients 2. Apply condiments 1. BLT 3. Apply cheese 2. Veggie burger 4. Cook it 3. Reuben 5. Wrap it This example is adapted from Head First Design Patterns, Freeman and Robson, O'Reilly Media A DESIGN FOR A SANDWICH Sandwich prepareIngredients(); applyCondiments(); applyCheese(); cook(); wrapIt(); Veggie BLT Reuben Burger A DESIGN FOR SANDWICH SHOP Sandwich SandwichShop prepareIngredients(); applyCondiments(); orderSandwich(); applyCheese(); cook(); wrapIt(); Veggie BLT Reuben Burger ORDERING PROCESS: CREATING SANDWICHES Sandwich orderSandwich (String myOrder) { Sandwich sandwich; if (myOrder.equals("BLT") { sandwich = new BLT(); } else if (myOrder.equals("VeggieBurger") { sandwich = new BLT(); } else if (myOrder.equals("Reuben") { sandwich = new Reuben(); } sandwich.prepareIngredients(); sandwich.applyCondiments(); Sandwich.applyCheese(); sandwich.cook(); sandwich.wrapIt(); } OVER TIME, THE MENU CHANGES. SANDWICHES COME. SANDWICHES GO. Sandwich orderSandwich (String myOrder) { Sandwich sandwich; if (myOrder.equals("BLT") { 1. BLT sandwich = new BLT(); } else if (myOrder.equals("VeggieBurger") { 2. Veggie burger sandwich = new BLT(); 3. Reuben } else if (myOrder.equals("Reuben") { 4. Turkey Club sandwich = new Reuben(); 5. Falafel } sandwich.prepareIngredients(); sandwich.applyCondiments(); Sandwich.applyCheese(); sandwich.cook(); sandwich.wrapIt(); } OVER TIME, THE MENU CHANGES. SANDWICHES COME. SANDWICHES GO. Sandwich orderSandwich (String myOrder) { Sandwich sandwich; if (myOrder.equals("BLT") { sandwich = new BLT(); } else if (myOrder.equals("VeggieBurger") { sandwich = new BLT(); This changes This } else if (myOrder.equals("Reuben") { sandwich = new Reuben(); } sandwich.prepareIngredients(); sandwich.applyCondiments(); Sandwich.applyCheese(); sandwich.cook(); This doesn't This sandwich.wrapIt(); } SO CREATION ADJUSTS Sandwich orderSandwich (String myOrder) { Sandwich orderSandwich (String myOrder) { Sandwich sandwich; Sandwich sandwich; if (myOrder.equals("BLT") { if (myOrder.equals("BLT") { sandwich = new BLT(); sandwich = new BLT(); } else if (myOrder.equals("VeggieBurger") { } else if (myOrder.equals("VeggieBurger") { sandwich = new BLT(); sandwich = new BLT(); This changes This } else if (myOrder.equals("Reuben") { } else if (myOrder.equals("Reuben") { sandwich = new Reuben(); sandwich = new Reuben(); } } else if (myOrder.equals("TurkeyClub") { sandwich = new TurkeyClub(); sandwich.prepareIngredients(); } else if (myOrder.equals("Falafel") { sandwich.applyCondiments(); sandwich = new Falafel(); Sandwich.applyCheese(); } sandwich.cook(); sandwich.prepareIngredients (); sandwich.wrapIt(); sandwich.applyCondiments (); This doesn't This } sandwich.cook (); Sandwich.wrapIt (); SO CREATION ADJUSTS Sandwich orderSandwich (String myOrder) { Sandwich orderSandwich (String myOrder) { Sandwich sandwich; So you encapsulate what changes if (myOrder.equals("BLT") { sandwich = new BLT(); } else if (myOrder.equals("VeggieBurger") { FACTORY handles sandwich = new BLT(); This changes This } else if (myOrder.equals("Reuben") { object creation sandwich = new Reuben(); } else if (myOrder.equals("TurkeyClub") { sandwich = new TurkeyClub(); } else if (myOrder.equals("Falafel") { sandwich = new Falafel(); } sandwich.prepareIngredients (); sandwich.applyCondiments (); sandwich.cook (); Sandwich.wrapIt (); SIMPLE SANDWICH FACTORY public class SimpleSandwichFactory { public Sandwich createSandwich (String sandwichType) { Sandwich sandwich = null; if (sandwichType.equals("BLT") { sandwich = new BLT(); } else if (sandwichType.equals("VeggieBurger") { sandwich = new BLT(); } else if (sandwichType.equals("TurkeyClub") { sandwich = new TurkeyClub(); } else if (sandwichType.equals("Falafel") { sandwich = new Falafel(); } return sandwich; } } SANDWICH SHOP public class SandwichShop { SimpleSandwichFactory myFactory; public SandwichShop(SimpleSandwichFactory factory) { myFactory = factory; } public Sandwich orderSandwich (String myOrder) { Sandwich sandwich; sandwich = myFactory.createSandwich(myOrder); sandwich.prepareIngredients (); sandwich.applyCondiments (); Sandwich.applyCheese (); sandwich.cook (); sandwich.wrapIt (); return sandwich; } } A DESIGN FOR SANDWICH SHOP Sandwich SandwichShop SimpleSandwichFactory prepareIngredients(); applyCondiments(); orderSandwich(); createSandwich(); applyCheese(); wrapIt(); Veggie BLT Reuben Burger A DESIGN FOR SANDWICH SHOP Sandwich SandwichShop SimpleSandwichFactory Sandwich is an prepareIngredients(); abstract class or applyCondiments(); an interface orderSandwich(); createSandwich(); applyCheese(); wrapIt(); Materials and methods for each type of sandwich are given (or Veggie over-ridden) here BLT Reuben Burger NOW LET’S GO NATIONAL, BUT ALLOW REGIONAL FLEXIBILITY Suppose we franchise the sandwich shop to open stores in Seattle, Silicon Valley and Charlotte. Each area might want to specialize sandwiches to reflect local tastes. Shops will create their own version of sandwiches, which means createSandwich() will need to become shop specific. The steps to make a sandwich are the same everywhere, but 1. the ingredients may differ 2. how these steps are implemented may differ. public abstract class Sandwich { String bread; String[] ingredients; WHICH LEADS TO… String[] cheese; String[] condiments; public abstract class SandwichShop { String[] toppings; abstract Sandwich createSandwich(); // default behaviors } prepareIngredients(); applyCondiments(); applyCheese(); public class BOSSandwichShop extends SandwichShop { cook(); wrapIt(); Sandwich createSandwich(String sandwichType) { } if (sandwichType.equals("BLT")) { return new BOSBLT(); }

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    50 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us