Design Mode note Abstract Factory mode Abstract Factory
// Abstract Factory ---- object Creation Mode
/*
1: intent: provide an interface for creating a series of related or mutually dependent objects without the need to define their specific classes.
2: alias: Kit
3: Motivation
4: Adaptability:
1> A system must be independent of the creation, combination, and representation of its products.
2> A system must be configured by one of multiple product generations.
3> when you want to emphasize the design of a series of related product objects for joint use.
4> when you provide a product library, you only want to display their interfaces instead of implementations.
5: structure:
AbstractFactory: <----------------------------------------------- Client
CreateProductA () |
CreateProductB () |
| AbstractProductA <----------------
|
----------------- ----------- |
|
ConcreteFactory1: ConcreteFactor2:---> ProductA2 ProductA1 <---|
CreateProductA () |
CreateProductB () |
| AbstractProductB <---------------
|
| --------- |
|
|--> ProductB2 ProductB1 <---|
|--|
6: participants:
1> AbstractFactory: declares an operation interface for creating abstract product objects.
2> ConcreteFactory: creates a specific product object.
3> AbstractProduct: declares an interface for a type of product object.
4> ConcreteProduct:
1) define a product object created by a specific factory that will be responded.
2) Implement the AbstractProduct interface.
5> Client: Only interfaces declared by the AbstractFactory and AbstractProduct classes are used.
7: collaboration:
1> Create a ConcreteFactory class instance at runtime. This specific factory creation has
Specific product objects. To create different product objects, customers should use different factories.
2> AbstractFactory delays the creation of product objects to its ConcreteFactory subclass.
8: effect:
1> advantages:
1) It separates specific classes:
The AbstractFactory mode helps control the class of an object created by an application.
Because each factory encapsulates the responsibility and process for creating product objects, it separates the implementation of customers from classes. Customer pass
Abstract interfaces can be used to operate instance objects. The product class names are separated in the implementation of specific factories.
2) It makes it easy to switch product series:
When the customer wants to change the product series for use, they only need to change the specific factory.
It is the factory that is changed during initialization, And the rest does not need to be changed, because the customer uses Abstract Factory interfaces.
3) conducive to product consistency:
When a product object in a series is designed to work together, an application can only use objects in the same series at a time.
AbstractFactory is easy to implement.
2> disadvantages:
It is difficult to support new types of products:
It is difficult to add a product in a product series, so you need to change the interface of AbstractFactory.
Other subclasses also need to be changed.
9: Implementation:
1> factory as a single piece:
In an application, only one ConcreteFactory instance is required for each product series. Therefore
The factory is usually better implemented as a Singleton.
2> Create a product:
AbstractFactory only declares one interface for creating a product. The actual product creation is implemented by ConcreteProduct.
The most common method is to define a factory method for each product. A factory will redefine the products specified by the factory for each product.
The second method is to use the Prototype mode. This method can be used when there are many series.
3> define a scalable Factory:
An insecure method is to input a parameter (identifier) when creating a product, select the product type to be created, and return
Generic Type pointers (this is not safe, and all product types must have common base classes and the same interface ).
10: code implementation :*/
Class MazeFactory
{
Public:
MazeFactory ();
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 * MazeGame: CreateMaze (MazeFactory & factory)
{
Maze * aMaze = factory. MakeMaze ();
Room * r1 = factory. MakeRoom (1 );
Room * r2 = factory. MakeRoom (2 );
...
Return aMaze;
}
// Check out the Bombed Factory
Wall * BombedMazeFactory: MakeWall () const
{
Return new BombedWall;
}
Room * BombedMazeFactory: MakeRoom (int n) const
{
Return new RoomWithABomb (n );
}
// You can create different products by changing the factory at the time of creation.