210 likes | 462 Vues
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
E N D
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 • 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
The Factory Method Design Pattern (3) • Non-software example • Injection Molding • Mold determines actual object • Injection system just processes the mold
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
The Factory Method Design Pattern (5) • Abstract Description • Object Factory Method • The design separates client from creator • Inheritance is used but not emphasized
The Factory Method Design Pattern (6) • Abstract Description • Object Factory Method Sequence Diagram
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
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
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
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
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
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
The Abstract Factory Design Pattern (3) • Non-software example • Sheet metal stamping equipment • Stamp all roof, trunk, left door, right door to match
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
The Abstract Factory Design Pattern (5) • Abstract Description • Collaborations
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
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
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
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
The Abstract Factory Design Pattern (10) • Java API Usage • java.awt.Toolkit Client Abstract Factory