Factory mode: Simple Factory mode, factory method mode, abstract Factory mode
1), Simple Factory mode: The main feature is the need to make judgments in the factory class, thereby creating the corresponding products, when adding new products, the need to modify the factory class.
typedefenum{T80=1, T99} Tanktype;classtank{ Public: Virtual voidMessage () =0;};classTANK80: Publictank{ Public: voidmessage () {cout<<"Tank80"<<Endl; }};classTank99: Publictank{ Public: voidmessage () {cout<<"Tank99"<<Endl; }};classtankfactory{ Public: Tank*Createtank (tanktype type) {Switch(type) { Case 1: return NewTank80 (); Case 2: return NewTank99 (); default: returnNULL; } }};
2), factory method mode: Refers to the definition of an interface to create an object, so that subclasses decide which class to instantiate, Factory method causes the instantiation of a class to be deferred to its subclasses.
Intent: Define an interface that creates an object, letting its subclasses decide which factory class to instantiate, and the factory pattern to defer its creation to subclasses.
Main solution: The main solution to the problem of interface selection.
When to use: we explicitly plan to create different instances under different conditions.
How to solve: let its sub-class implement the Factory interface, return is also an abstract product.
Key code: The creation process executes in its subclasses.
Cons: Each additional product requires an additional object factory. The factory method pattern needs to define more classes than the simple factory model.
classtank{ Public: Virtual voidMessage () =0;};classTANK80: Publictank{ Public: voidmessage () {cout<<"Tank80"<<Endl; }};classTank99: Publictank{ Public: voidmessage () {cout<<"Tank99"<<Endl; }};classtankfactory{ Public: Virtualtank* Createtank () =0;};classTank80factory: Publictankfactory{ Public: Tank*Createtank () {return NewTank80 (); }};classTank99factory: Publictankfactory{ Public: Tank*Createtank () {return NewTank99 (); }};
3), Abstract Factory mode: Provides a specific class that creates a series of related or interdependent object interfaces without specifying them.
Intent: Provides an interface that creates a series of related or interdependent objects without specifying their specific classes.
Main solution: The main solution to the problem of interface selection.
When to use: System products have more than one product family, and the system only consumes the products of one of the families.
How to solve: within a product family, define multiple products.
Key code: Aggregating multiple similar products in one factory.
Disadvantage: Product family expansion is very difficult, to add a series of a product, both in the abstract Creator Riga code, but also in the concrete inside add code.
classtank{ Public: Virtual voidMessage () =0;};classTANK80: Publictank{ Public: voidmessage () {cout<<"Tank80"<<Endl; }};classTank99: Publictank{ Public: voidmessage () {cout<<"Tank99"<<Endl; }};classplain{ Public: Virtual voidMessage () =0;};classPlain80: Publicplain{ Public: voidmessage () {cout<<"Plain80"<<Endl; }};classPlain99: Publicplain{ Public: voidmessage () {cout<<"Plain99"<<Endl; }};classfactory{ Public: Virtualtank* Createtank () =0; Virtualplain* Createplain () =0;};classFACTORY80: Publicfactory{ Public: Tank*Createtank () {return NewTank80 (); } Plain*Createplain () {return NewPlain80 (); }};classFactory99: Publicfactory{ Public: Tank*Createtank () {return NewTank99 (); } Plain*Createplain () {return NewPlain99 (); }};
Note: The above for my study notes, if there is a mistake to welcome points, thank you.
Design Pattern Learning Notes (c + +)---Factory mode