Intent: Define an interface for creating an object that allows subclasses to perceive which class to instantiate.
Applicability: 1. A class does not know when it must create an object.
2. A class wants to have its child class specify the object it creates.
3. When a class delegates the responsibility of creating an object to one of several helper subclasses, and wants to place that helper subclass as a proxy, this information is localized.
Effect: 1. Provides an extended version of the object.
2. Connect the parallel class hierarchy.
Today I learn the factory method mode, feel to understand from a few factory patterns together, so I tell you the whole idea.
At first, the boss demanded that the product be produced according to Customer's wishes. So there is a simple factory, the code is as follows:
#ifndef _product_#define_product_#include<string>using namespacestd;classabsproduct{ Public: Absproduct () {}Virtual stringGetProduct () =0;};classProduct_a: Publicabsproduct{ Public: Product_a () {}Virtual stringGetProduct () {return "product_a";}};classProduct_b: Publicabsproduct{ Public: Product_b () {}Virtual stringGetProduct () {return "Product_b";}};classProduct_c: Publicabsproduct{ Public: Product_c () {}Virtual stringGetProduct () {return "Product_c";}};#endifProduct Part Code
Factory Part code:
#ifndef _factory_#define_factory_#include"Product.h"classfactory{ Public: Factory () {} absproduct* MAKEPRODUCT (Charch) { Switch(CH) { Case 'A': return Newproduct_a; Break; Case 'B': return NewProduct_b; Break; Case 'C': return NewProduct_c; Break; default: Break; } }};#endif
Implementation section:
#include <iostream>#include<string>using namespacestd; #include"Factory.h"#include"Product.h"intMain () {Factory Factory; Absproduct* Product = Factory. Makeproduct ('A'); cout<<product->getproduct () <<Endl; Product= Factory. Makeproduct ('B'); cout<<product->getproduct () <<Endl; Product= Factory. Makeproduct ('C'); cout<<product->getproduct () <<Endl; return 0;}
One day, the boss asked: the new product Product_d added to the production. Although the simple factory model satisfies the product according to the requirement,
but the new product cannot be expanded . The factory method mode is implemented:
First, add product_d to the product section:
class Product_d: public absproduct{public: product_d () {} virtual string GetProduct () {return"product_d";}};
The factory section should read:
classabsfactory{ Public: Absfactory () {}Virtualabsproduct* makefroduct () =0;};classFactory_a: Publicabsfactory{ Public: Factory_a () {}Virtualabsproduct* makefroduct () {return Newproduct_a;}};classFactory_b: Publicabsfactory{ Public: Factory_b () {}Virtualabsproduct* makefroduct () {return Newproduct_b;}};classFactory_c: Publicabsfactory{ Public: Factory_c () {}Virtualabsproduct* makefroduct () {return NewProduct_c;}};classFactory_d: Publicabsfactory{ Public: Factory_d () {}Virtualabsproduct* makefroduct () {return Newproduct_d;}};Factory Method Factory part code
Implementation section:
#include <iostream>#include<string>using namespacestd; #include"Factory.h"#include"Product.h"intMain () {absfactory* Factory =Newfactory_a; Absproduct* Product = factory->makefroduct (); cout<<product->getproduct () <<Endl; Factory=NewFactory_b; Product= factory->makefroduct (); cout<<product->getproduct () <<Endl; Factory=NewFactory_c; Product= factory->makefroduct (); cout<<product->getproduct () <<Endl; Factory=NewFactory_d; Product= factory->makefroduct (); cout<<product->getproduct () <<Endl; return 0;}
The boss asked again, "Now our company is the flagship of these four products, you have to update these products regularly." ”
In this way, the factory method satisfies the requirement of producing the corresponding products according to customer's requirements, and also satisfies the requirement of expanding new products .
but not to meet the new product replacement . So the abstract factory model came out ...
For our example, assume that the company's products a1,b1,c1,d1 for a product, A2,B2,C2,D2 for the two phase of the product.
Then A1,a2,a3 ... For the same family product. And A1,b1,c1,d1 is the same grade product. If only the factory produces the same grade
Product, it is the factory method mode. Abstract Factory mode if a product of different grades is produced.
--Novice beginner, hope the big God pointing--
Factory method Mode