0% found this document useful (0 votes)
128 views

Factory Method

The document discusses the Factory Method design pattern. Factory Method defines an interface for creating objects but allows subclasses to determine which class to instantiate. It lets a class delegate instantiation to subclasses. The pattern name, problem it addresses, structure, participants, collaborations, consequences and sample code are described. Factory Method is useful when a class doesn't know which class to instantiate or wants subclasses to specify object creation.

Uploaded by

CHANDRA BHUSHAN
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Factory Method

The document discusses the Factory Method design pattern. Factory Method defines an interface for creating objects but allows subclasses to determine which class to instantiate. It lets a class delegate instantiation to subclasses. The pattern name, problem it addresses, structure, participants, collaborations, consequences and sample code are described. Factory Method is useful when a class doesn't know which class to instantiate or wants subclasses to specify object creation.

Uploaded by

CHANDRA BHUSHAN
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CS590L Workshop II

Design Patterns - Factory Method


Dhananjay Indurkar di5x7@umkc.edu

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

class just to create a particular concreteProduct object.

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

-when the Creator class is concrete class and

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.

Factory Method - Templates


class Creator { public: virtual Product* CreateProduct() = 0; }; /* */ template <class TheProduct> class StandardCreator: public Creator { public: virtual Product* CreateProduct(); }; /* */ template <class TheProduct> Product* StandardCreator<TheProduct> ::CreateProduct () { return new TheProduct; }

Factory Method - Templates


class MyProduct : public Product { public: MyProduct(); // ... }; StandardCreator <MyProduct> myCreator ;

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy