Factory Method
Factory Method
Design Patterns
Recap Design Pattern
A design pattern systematically names, explains, and evaluates an important and recurring design in object-oriented systems.
Design Patterns
Why use design patterns Design patterns make it easier to reuse successful designs and architectures. help make choose design alternatives that make a system reusable and avoid alternatives that compromise reusability. help improve documentation and maintenance of existing systems
Design Patterns
Essential elements of a design pattern Pattern name Problem description Solution Consequences There are 23 design patterns in all.
Design Patterns
Describing a Design Pattern Pattern name and classification Intent Alias Motivation Applicability Structure
Design Patterns
Describing a Design Pattern Participants Collaborations Consequences Implementation Sample code Known uses Related patterns
Design Patterns
Classification of design patterns Purpose Scope
Design Patterns
Factory Method
Defines an interface for creating an object, but lets the subclasses decide which class to instantiate. It lets a class defer instantiation to subclasses. Also known as Virtual Constructor
Factory Method
Motivation Abstract classes used by frameworks to define and maintain relationships between objects. The framework must instantiate classes, but it knows only about abstract classes which can not be instantiated. Use Factory Method. It encapsulates the knowledge of which subclass to create.
Factory Method
Applicability
Factory method design pattern could be used when a class cant anticipate the class of objects it must create a class wants its subclasses to specify the objects it creates classes delegate responsibility to one of several helper subclasses, and we want to localize the knowledge of which helper subclass is the delegate.
Factory Method
Structure
Factory Method
Participants Product - defines the interface of objects the factory method
creates
Concrete product - implements the object interface Creator - declares the factory method Concrete creator - overrides the factory method to create an instance
with the desired attributes
Factory Method
Collaborations The creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate subclass
Factory Method
Consequences Factory methods eliminate the need to bind application-specific classes into code. Potential disadvantage - clients might have to subclass the creator
Factory Method
Additional consequences
Provides hooks for subclasses Connects parallel class hierarchies.
Factory Method
Implementation Two major varieties - when the Creator class is abstract class and
does not provide a default implementation provides a default implementation
Factory Method
Parameterized factory methods
Example of a parameterized factory method class Creator { public: virtual Product* Create(ProductId); }; /* */ Product* Creator::Create (ProductId id) { if (id == MINE) return new MyProduct; if (id == YOURS) return new YourProduct; // repeat for remaining products... } return 0;
Factory Method
Using Templates
Workaround for the problem that requires to subclass just to create the appropriate objects.
Sample Code
class MazeGame { public: Maze* CreateMaze(); /* */ // factory methods: /* */ virtual Maze* MakeMaze() const { return new Maze; } virtual Room* MakeRoom(int n) const { return new Room(n); } virtual Wall* MakeWall() const { return new Wall; } virtual Door* MakeDoor(Room* r1, Room* r2) const { return new Door(r1, r2); } };
Sample Code ..
Maze* MazeGame ::CreateMaze () { Maze* aMaze = MakeMaze(); /* */ Room* r1 = MakeRoom(1); Room* r2 = MakeRoom(2); Door* theDoor = MakeDoor(r1, r2); /* */ aMaze ->AddRoom(r1); aMaze ->AddRoom(r2); /* */ ..
Sample Code ..
r1->SetSide(North, MakeWall()); r1->SetSide(East, theDoor); r1->SetSide(South, MakeWall()); r1->SetSide(West, MakeWall()); /* */ r2->SetSide(North, MakeWall()); r2->SetSide(East, MakeWall()); r2->SetSide(South, MakeWall()); r2->SetSide(West, theDoor); /* */ return aMaze; }
Factory Method
Related Patterns Abstract Factory pattern often implemented with Factory Methods Template Methods contain calls to Factory Methods