Direct point, the factory model is to take over the creation of objects, at the same time, new () can do the additional exception handling, initialization and other operations, but also reduce the coupling between the modules, easy to maintain and expand.
Based on the complexity of the object creation, it can be divided into simple Factory mode, factory law mode and abstract Factory mode.
First, simple Factory mode
ProductA, PRODUCTB, and PRODUCTC inherit from the product virtual class, and the Show method is a self-describing of different products; factory relies on ProductA, PRODUCTB, and PRODUCTC, Factory create different product objects based on different conditions.
typedef enum PRODUCTTYPETAG{TYPEA,TYPEB,TYPEC}PRODUCTTYPE;//Here is the product classclass 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 Productc:public product{public:void Show () {cout<< "I ' m PRODUCTC" <<endl;}};/ /Here are the Factory classclass factory{public:product* createproduct (producttype type) {switch (type) {case Typea:return New ProductA (), Case Typeb:return New PRODUCTB (), Case Typec:return new PRODUCTC ();d Efault:return NULL;}};
A simple wrapper to create a three-kind object that inherits from the virtual base class product, the constraints are: ProductA, PRODUCTB, PRODUCTC are all types that inherit from product, and can take advantage of polymorphic attributes.
In addition, there is only one product subclass, which also belongs to the simple factory model.
Ii. Model of Factory law
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 the production of PRODUCTD products; Then, first, you need to add a new product type identification in the product enumeration type, and then, Modify the switch structure code in the factory class. Yes, this modification to the code, the original code changes a large amount. For a variety of reasons, the simple factory model is less used in actual projects.
What should I do then?
"Head First" in the production of pizza as an example, defined a virtual base class Pizzastore, containing a pure virtual function Createpizza (), by inheriting this virtual base class overload Createpizza () can be produced by the taste of different pizza.
The factory method model adds an abstraction layer to the "factory" based on the simple factory model. Abstract the common actions of the factory, as abstract classes, and the specific behavior of the subclass itself to achieve, let the sub-class to decide what products to produce.
In this way, to add a product productc, simply inherit the abstraction Layer factory build a new factory FACTORYC, to create new products. The original factory does not need to be modified. The meaning of the factory method pattern is to define a factory interface that creates the product objects, deferring the actual creation to subclasses. The Core factory class is no longer responsible for product creation, so that the core class becomes an abstract factory role and is responsible only for the interfaces that a specific factory subclass must implement, and the benefit of further abstraction is that the factory method pattern allows the system to introduce new products without modifying the specific factory roles.
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 ();}};
III. Abstract Factory Model
We may think that later products will be more and more, the establishment of factories will be more and more, factories have grown, factories have become messy and difficult to manage; Because the factory method pattern creates objects that inherit product, each factory in the factory method pattern can only create a single kind of product, When it comes to producing a completely new product (not inheriting from product), the discovery of a factory approach is powerless.
The factory method model is suitable for a single type of product structure, providing an interface for a class of products, while the abstract factory approach is suitable for applications where there are many types of products, mainly used to create a set of products (with multiple kinds) related, and to provide the interfaces created for them; When you have multiple abstract roles, Abstract factories can be useful.
Class producta{public:virtual void Show () = 0;}; Class Producta1:public producta{public:void Show () {cout<< "I m ProductA1" <<endl;}}; Class Producta2:public producta{public:void Show () {cout<< "I ' m ProductA2" <<endl;}};/ /Product bclass productb{public:virtual void Show () = 0;}; Class Productb1:public productb{public:void Show () {cout<< "I m ProductB1" <<endl;}}; Class Productb2:public productb{public:void Show () {cout<< "I ' m ProductB2" <<endl;}};/ /Factoryclass factory{public:virtual producta *createproducta () = 0;virtual PRODUCTB *createproductb () = 0;}; Class Factory1:public Factory{public:producta *createproducta () {return new ProductA1 (); PRODUCTB *CREATEPRODUCTB () {return new ProductB1 ();}}; Class Factory2:public Factory{producta *createproducta () {return new ProductA2 (); PRODUCTB *CREATEPRODUCTB () {return new ProductB2 ();}};
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[C + + design mode] Factory Factory mode