Design Pattern-Factory pattern)
Factory Model
Let's take a look at this situation: Suppose we are doing a large multiplayer online game with hundreds of monsters in it. At this time, we can easily think of using polymorphism to implement the monster category. Abstract The common properties of monsters. Different monster classes inherit this base class. Well, if the monster class is encapsulated in this way, the new XX monster class is required wherever the monster class is needed, there will be several such problems: 1, the customer programmer must know the name of the actual subclass (when the system is complex, naming will be a very difficult problem to handle, in order to handle possible name conflicts, some naming may not be very readable and memorable, So let alone individual programmers may be better off.) 2. Program scalability and maintenance become increasingly difficult.
The so-called factory mode is to manage the creation of these derived class objects, that is, many objects of the same category need to be created with a new class to manage the creation of these objects, encapsulate the new operation into the interior, so that the structure is clearer. This management class is our factory. To create specific external objects, we always use this factory and do not need to create new ones by ourselves, interfaces are unified externally.
Factory mode structure:
Note: The factory model also brings about a problem, that is, as long as a new derived object is added, the factory management class needs to be changed accordingly. In this way, FactZ functions? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcnkgtcS907/examples + rzbtcShozwvcD4NCjxoMiBpZD0 =" example "> example
Suppose there is a McDonald's, we buy food, then this McDonald's is equivalent to a factory, all kinds of food is equivalent to a derived class object, and what we want to eat, just go to the McDonald's store to get, we don't need to worry about the specific processing process.
Various Foods:
//// Product. h // DesignMode /// Created by app05 on 15-10-28. // Copyright (c) 2015 app05. All rights reserved. // # ifndef _ DesignMode _ Product __# define _ DesignMode _ Product _ class Product {public: virtual ~ Product () = 0; protected: Product (); private:}; class ChickenProduct: public Product {public: ChickenProduct ();~ ChickenProduct () ;}; class ChipsProduct: public Product {public: ChipsProduct ();~ ChipsProduct () ;};# endif/* defined (_ DesignMode _ Product __)*/
//// Product. cpp // DesignMode /// Created by app05 on 15-10-28. // Copyright (c) 2015 app05. All rights reserved. // # include Product. h # include
Using namespace std; Product: Product () {} Product ::~ Product () {} ChickenProduct: ChickenProduct () {cout <I want a chicken wing... <
McDonald's factory:
//// Factory. h // DesignMode /// Created by app05 on 15-10-28. // Copyright (c) 2015 app05. All rights reserved. // # ifndef _ DesignMode _ Factory __# define _ DesignMode _ Factory _ enum ProductType {CHICKEN, CHIPS}; class Product; class Factory {public: factory ();~ Factory (); Product * CreateProduct (ProductType type) ;};# endif/* defined (_ DesignMode _ Factory __)*/
//// Factory. cpp // DesignMode /// Created by app05 on 15-10-28. // Copyright (c) 2015 app05. All rights reserved. // # include Factory. h # include Product. h # include
Using namespace std; Factory: Factory () {cout <create Factory <endl;} Factory ::~ Factory () {} Product * Factory: CreateProduct (ProductType type) {switch (type) {case CHICKEN: return new ChickenProduct (); break; case CHIPS: return new ChipsProduct (); break ;}}
We call it food:
//// Main. cpp // DesignMode /// Created by app05 on 15-10-28. // Copyright (c) 2015 app05. All rights reserved. // # include Factory. h # include Product. h # include
Using namespace std; int main (int argc, const char * argv []) {Factory * fac = new Factory (); Product * p1 = fac-> CreateProduct (CHICKEN ); product * p2 = fac-> CreateProduct (CHIPS); return 0 ;}