Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.

Similar presentations


Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design."— Presentation transcript:

1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (3) –Non-software example Injection Molding –Mold determines actual object –Injection system just processes the mold

4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (5) –Abstract Description Object Factory Method –The design separates client from creator –Inheritance is used but not emphasized

6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (6) –Abstract Description Object Factory Method Sequence Diagram

7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Abstract Factory Design Pattern (3) –Non-software example Sheet metal stamping equipment –Stamp all roof, trunk, left door, right door to match

14 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Abstract Factory Design Pattern (5) –Abstract Description Collaborations

16 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 the createShape() request

19 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 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 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Abstract Factory Design Pattern (10) –Java API Usage java.awt.Toolkit Abstract Factory Client


Download ppt "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design."

Similar presentations


Ads by Google