Problem description
Before the C + + design pattern--Simple factory model, due to the limitations of the simple factory model, such as: The factory can now produce producta, PRODUCTB and PRODUCTC three kinds of products, at this time, the need to increase production PRODUCTD products; First, you need to add a new product type identity to the product enumeration type, and then modify the switch structure code in the factory class. Yes, this modification of the code, the original code changes large, easy to produce coding errors (although very simple, if the project is big, error is inevitable!!! )。 This modification of the code is the most primitive, most barbaric modification, which is essentially not an extension of the code. At the same time, since the existing functions have been modified, the previous tests will be invalid, all tests will need to be restarted, all the code needs to be overwritten. This, increased costs, can not improve the efficiency of things in the company is absolutely not allowed (unless the fatuous pm). For a variety of reasons, the simple factory pattern is less used in actual projects. So what should we do? What do we do? The need to minimize the impact of the original code, while the original functionality can be extended.
UML Class Diagram
So today's introduction of the factory method model, on the grand debut. It is simply the expansion of the simple factory model, in the introduction of GOF, they are merged together, and I was separate to explain, is to distinguish between the pros and cons of the two, for everyone in the actual project to better grasp and application. The factory method model adds an abstraction layer to the factory based on the simple factory pattern. Abstract the common action of the factory as an abstract class, and the specific behavior is implemented by subclasses themselves, so that subclasses decide what products to produce.
As pictured, Factorya is responsible for production PRODUCTA,FACTORYB concentration is responsible for the production of Productb,factorya and Factoryb there is no relationship between; if later, if you need to produce PRODUCTC, We can create a FACTORYC factory class that is focused on producing PRODUCTC products. Since there is no relationship between Factorya, Factoryb, and Factoryc, when joining FACTORYC joins, there is no effect on Factorya and factoryb work, when the code is tested, The need for unit testing of FACTORYC and PRODUCTC alone, while Factorya and Factoryb do not have to be tested, can save a lot of uninteresting and tasteless testing work.
Applicable occasions
The meaning of the factory method pattern is to define a factory interface that creates the product objects, deferring the actual creation to the subclass. The Core factory class is no longer responsible for the creation of the product, so the core class becomes an abstract factory role that is responsible only for the interfaces that must be implemented by the specific factory subclass, so the benefit of further abstraction is that the factory method model allows the system to introduce new products without modifying the specific factory role.
1. In the early stages of design, considering that the product will be expanded in the later period, the factory method model can be used.
2. When the product structure is more complex, the factory method model can be used;
Because design patterns are used in detailed design, you need to make a decision, so you need to weigh a number of factors and not use design patterns for design patterns.
Code implementation:
* * * * * * filename:factorymethodpatterndemo * * author:jelly Young * * * DATE:2013/11/18 * * Description:more INF
Ormation, please go to http://www.jb51.net * * * #include <iostream> using namespace std;
Class Product {public:virtual void show () = 0;};
Class Producta:public Product {public:void Show () {cout<< "I ' m producta" <<endl;
}
};
Class Productb:public Product {public:void Show () {cout<< "I ' m PRODUCTB" <<endl;
}
};
Class Factory {public:virtual Product *createproduct () = 0;};
Class Factorya:public Factory {public:product *createproduct () {return new producta ();
}
};
Class Factoryb:public Factory {public:product *createproduct () {return new PRODUCTB ();
}
};
int main (int argc, char *argv []) {Factory *factorya = new Factorya ();
Product *producta = Factorya->createproduct ();
Producta->show ();
Factory *factoryb = new Factoryb (); Product *produCtB = Factoryb->createproduct ();
Productb->show ();
if (Factorya!= NULL) {delete Factorya;
Factorya = NULL;
} if (ProductA!= NULL) {delete producta;
ProductA = NULL;
} if (Factoryb!= NULL) {delete factoryb;
Factoryb = NULL;
} if (PRODUCTB!= NULL) {delete productb;
PRODUCTB = NULL;
return 0; }