Design Pattern Note Factory method Factory Methon
// Factory method ------- object Creation Mode
/*
1: Intention:
Defines an interface used to create objects, so that the subclass determines which class to instantiate.
Factory Method delays the instantiation of a class to its subclass.
2: alias: virtual constructor)
3: motivation:
4: Applicability:
1> when a class does not know the class of the object it must create.
2> when a class wants its subclass to specify the object it creates.
3> when the class delegates the responsibility of creating an object to one of the multiple help sub-classes, and which help sub-classes do you Want to delegate
This information is localized.
5: structure:
Product Creator:
| FactorMethod ()
| AnOperation () {product = FactoryMethod ();...}
|
ConcreteProduct <---ConcreteCreator:
FactoryMethod ()
{Return new ConcereteProduct ;}
6: participants:
1> Product: defines the interface of the object created by the factory method.
2> ConcreteProduct: implements the Product interface.
3> Creator:
1) Declare the factory method, which returns an object of the Product type. Creator can also define
The default Implementation of the factory method, which can return a default ConcreteProduct object.
2) You can call the factory method to return a ConcreteProduct instance.
4> ConcreteCreator: redefines the factory method to return a ConcreteProduct instance.
7: Collaboration: Creator depends on its subclass to define the factory method, so it returns an appropriate ConcreteProduct instance.
8: effect:
1> the factory method only provides one interface for processing the Product, so
It can be used with any user-defined ConcreteProduct class.
2> potential disadvantages: a customer has to create a Creator subclass to create a specific ConcreteProduct object.
3> two other effects:
1) provide hooks for sub-classes ). That is, you must first implement a default version of the object. sub-classes can be used as needed.
You can decide whether to overwrite the version of the parent class and create a specific object.
2) connect parallel class layers. (I think it's no different from hook. <Unknown tag>)
9: Implementation:
1> there are two major differences:
1) The Creator class is an abstract class and does not provide implementation of the factory method it declares:
Subclass definition implementation is required, because there is no reasonable default implementation, so avoid it
Do not instantiate unpredictable class issues.
2) Creator is a specific class and provides a default implementation for the factory method:
Subclass can change the class of the object instantiated by the parent class when necessary, so it is flexible.
2> Parameterized Factory method: provides a parameter to determine the specific class to be instantiated :*/
Class Creator
{
Public:
Virtual Product * Create (ProductId );
};
Product * Creator: Create (ProductId id)
{
If (id = MINE) returnnew MyProduct;
If (id = YOURS) returnnew YourProduct;
Return 0;
}
// Subclass can be redefined as follows:
Product * MyCreator: Create (ProductId id)
{
If (id = YOURS) returnnew MyProduct;
If (id = MINE) returnnew YourProduct;
If (id = THEIRS) returnnew TheirProduct;
Return Create: Create (id );
}
// Remember to call the Create statement of the parent class at the end, because the subclass may not be processed in some cases and must be handed over to the parent class for processing.
// 3> avoid creating subclasses using templates:
Template
Class StandardCreator: public Creator
{
Public:
Virtual Product * Create ()
{
Return new TheProduct;
}
};
/*
In this way, the customer can provide their own product categories into the template. We do not need to create a subclass of Creator.
4> naming conventions: it is a good habit to use naming conventions to clearly describe the factory method you are using.
10: Sample Code :*/
// Creator:
Class MazeGame
{
Public:
Maze * CreateMaze ();
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 );}
};
// Maze Room Wall Door is both Product
// ConcreteCreator:
Class BombedMazeGame: public MazeGame
{
Public:
BombedMazeGame ();
Virtual Wall * MakeWall () const
{Return new BombedWall ;}
Virtual Room * MakeRoom (int n) const
{Return new RoomWithABomb (n );}
};
// If you want a maze with Bombed, you only need to create a BombedMazeGame and call CreateMaze.