1 / 20

The Factory Method Design Pattern (1) A creational design pattern Separates out responsibility for construction of

The Factory Method Design Pattern (1) A creational design pattern Separates out responsibility for construction of concrete objects Intent Provide an interface for creating an object without revealing the object’s actual class. The Factory Method Design Pattern (2) Problem

eugene
Download Presentation

The Factory Method Design Pattern (1) A creational design pattern Separates out responsibility for construction of

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Factory Method Design Pattern (1) • A creational design pattern • Separates out responsibility for construction of concrete objects • Intent • Provide an interface for creating an object without revealing the object’s actual class

  2. The Factory Method Design Pattern (2) • Problem • Situation I: Wish to isolate a client that uses an object from the decision of what type of object to use • Situation II: Wish to specify as part of “interface” the possible constructors to be used • Recall that Java interfaces do not allow constructors

  3. The Factory Method Design Pattern (3) • Non-software example • Injection Molding • Mold determines actual object • Injection system just processes the mold

  4. The Factory Method Design Pattern (4) • Abstract Description • GoF presents one form – Class Pattern • Class Factory Method – uses inheritance • Object Factory Method – uses delegation • Class Factory Method Abstract class Abstract method Creates instance

  5. The Factory Method Design Pattern (5) • Abstract Description • Object Factory Method • The design separates client from creator • Inheritance is used but not emphasized

  6. The Factory Method Design Pattern (6) • Abstract Description • Object Factory Method Sequence Diagram

  7. The Factory Method Design Pattern (7) • Consequences • Class Version • isolates creator from the concrete product created • assumes client is derived from Creator class • Object Version • isolates client from creation altogether • doesn’t require any inheritance (uses implements) • allows factory to be changed dynamically

  8. The Factory Method Design Pattern (8) • Implementation Issues • Class Version • if Creator is abstract, it can provide default implementation of factory method • Object Version • How does the client know what concreteCreator to use? • New its own (ties to specific version) • Pass in as an argument • Use another factory method that produces factories • Either version can be implemented as a Singleton

  9. The Factory Method Design Pattern (9) • Example: Kaleidoscope • On each turn a new shape is added • Could have Kaleidoscope decide the new shape • public class Kaleidoscope { • … • public void turn() { • // Add new shape • switch (Randon.randInt() % • NUMSHAPETYPES){ • case 0: • theShapes.add(new Circle()); • break; • case 1: • theShapes.add(new Square()); • … • } • … • } Ties this decision to kaleidoscope

  10. The Factory Method Design Pattern (10) • Example: Kaleidoscope • Better to use object factory method • public class Kaleidoscope { • … • public void turn(){ • // Add new shape • theShapes.add(shapeFactory.createShape()); • … • } • … • } • } Returns a Shape – we don’t care which one

  11. The Abstract Factory Design Pattern (1) • A creational design pattern • Separates out responsibility for construction of concrete objects • Intent • Provide an interface for creating families of dependent objects without revealing the their actual classes

  12. The Abstract Factory Design Pattern (2) • Problem • Situation I: Wish to isolate a client from choosing a concrete implementation of an external subsystem • Situation II: Wish to provide a family of objects that are designed to work together, not intermixed • Situation III: Wish to provide a library of objects only revealing their interfaces not implementations

  13. The Abstract Factory Design Pattern (3) • Non-software example • Sheet metal stamping equipment • Stamp all roof, trunk, left door, right door to match

  14. The Abstract Factory Design Pattern (4) • Abstract Description • GoF presents one form – Object Pattern • Object Factory Method • An abstract factory is a collection of factory methods assigned to one central object

  15. The Abstract Factory Design Pattern (5) • Abstract Description • Collaborations

  16. The Abstract Factory Design Pattern (6) • Consequences • Isolates concrete classes from client • Ensures “like” products are used together • Makes it difficult to add new product types • Must change abstract factory interface • Must change all derived classes from abstract factory

  17. The Abstract Factory Design Pattern (7) • Implementation Issues • typically implemented as a Singleton • if Creator is abstract, it can provide default implementation of factory method • Strict “Object” version • Give abstract factory a reference to actual concrete factory • the “abstract” class implementing the abstract factory becomes concrete

  18. The Abstract Factory Design Pattern (8) • Example: Kaleidoscope • Want all shapes to be green, or blue, or … • Build an abstract factory for creating shapes • public abstract class ShapeFactory { • public abstract Circle createCircle(); • public abstract Rectangle createRectangle(); • … • } • public class GreenShapeFactory extends ShapeFactory { • public Circle createCircle(){ • return new Circle(Color.GREEN); • } • … • } • Of course, some factory must use this class to respond to thecreateShape()request

  19. The Abstract Factory Design Pattern (9) • Common Variations • Extensible Factory • replace createX() methods with generic create(X) • requires parameter to specify which concrete type • typically use strings as general purpose • requires single unifying return type

  20. The Abstract Factory Design Pattern (10) • Java API Usage • java.awt.Toolkit Client Abstract Factory

More Related