Document 97220

CS590L – Workshop II
Design Patterns
- Factory Method
Dhananjay Indurkar
[email protected]
Design Patterns
†Recap
„Design Pattern
A design pattern systematically names, explains,
and evaluates an important and recurring design
in object-oriented systems.
1
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.
2
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
3
Design Patterns
† Classification of design patterns
„ Purpose
„ Scope
Design Patterns
4
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.
5
Factory Method
† Applicability
Factory method design pattern could be used
when „ a class can’t 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
6
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
7
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.
8
Factory Method
† Implementation
„ Two major varieties - when the Creator class is abstract class and
does not provide a default implementation
-when the Creator class is concrete class and
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;
9
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;
}
10
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); }
};
11
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;
}
12
Factory Method
† Related Patterns
„ Abstract Factory pattern often
implemented with Factory Methods
„ Template Methods contain calls to
Factory Methods
13