Simple factory pattern
// In general, you have created a new object's method is a direct object to write their own business out of function
// But the reality needs, I do not want to create an object, I just want to make use of them. (To create a class of more complex)
// benefits 1, the client and the specific implementation class decoupling. 2, for some object creation process is more complicated cases, we do not consider these.
// harm, 1, a simple factory pattern, adding new features are implemented through the source code does not comply with the principle of opening and closing. 2, heavy duty this class, this class of problems, it will affect many factories use this module.
// abstract Fruit class AbstractFruit { public : Virtual void ShowName () = 0 ; }; // Apple class the Apple: public AbstractFruit { public : Virtual void ShowName () { cout << "I am the apple!" << endl; } }; // pear class pear:public AbstractFruit{ public: virtual void ShowName(){ cout << “我是鸭梨!” << endl; } }; //水果工厂 class FruitFactory{ public: static AbstractFruit* CreateFruit(string flag){ if(flag == "apple"){ return new Apple; } else if(flag == "pear"){ return new Pear; } else return NULL; } }; void test1(){ FruitFactory* factory = new FruitFactroy; AbstractFruit* apple = factory->CreateFruite("apple"); apple->ShowName(); AbstracFruit* banana = factory->CreateFruite("banana"); banana->ShowName(); delete factory; delete apple; delete banana; }
Factory Method pattern
Questions 1, type the number doubled, leading to more and more class, increase maintenance costs.
Benefits 1, in line with the principle of opening and closing
+ Simple factory pattern "on-off principle" = Factory Method
Advantages: 1, do not need to remember specific class name, and even the specific parameters do not remember.
2, to achieve the objects created and used separate.
3, scalability of the system will become very good, and the interface without modifying the original class.
Disadvantages: 1, increasing the number of classes in the system, and increase the complexity of understanding.
2, increasing the system's abstract and difficult to understand.
// abstract Fruit
class AbstractFruit { public : Virtual void ShowName () = 0 ; }; // Apple class the Apple: public AbstractFruit { public : Virtual void ShowName () { cout << << "I am the apple!" Endl; } }; // pear class pear: public AbstractFruit { public : Virtual void ShowName () { cout << "I am a pear!" << endl; } };
// class abstract factory
class AbstractFruitFactory {
public:
Virtual AbstractFruit CreateFruit * () = 0;
}
// Apple factory
class AppleFactory: public AbstractFruitFactory {
public:
Virtual AbstracFruit CreateFruit * () {
return new new the Apple;
}
};
//鸭梨工厂
class PearFactory:public AbstractFruitFactory{
public:
virtual AbstracFruit* CreateFruit(){
return new Pear;
}
};
the Test void () {
AbstractFruitFactory * Factory's = NULL;
AbstractFruit * Fruit = NULL;
// create an Apple factory
Factory's = new new AppleFactory;
Fruit = factory-> CreateFruit ();
fruit-> ShowName ();
the Delete Factory's;
the Delete Fruit;
// create a pear factory
Factory's = new new PearFactory;
Fruit = factory-> CreateFruit ();
fruit-> ShowName ();
the Delete Factory's;
the Delete Fruit;
}
Simple factory pattern and factory method pattern application scenarios
Simple factory pattern:
1, the object factory class is responsible for creating relatively small, because fewer objects created, the factory method will not cause too complex business logic.
2, the client only knows the parameters passed factory class, how to create objects do not care.
Factory Method pattern
1, the client does not know it needs class object.
2, abstract factory class to specify which object created by its subclasses.
Abstract factory pattern
Abstract factory for a product family, not a product hierarchy. Product family: the same origin or the same manufacturer, different functions. Product Grade: same function, different origin or vendors.
// abstract apple class AbstractApple { public : Virtual void ShowName () = 0 ; }; class ChineseApple: public AbstractApple { public : Virtual void ShowName () { cout << " Chinese apple " << endl; } }; // United States Apple class USAApple: public AbstractApple { public : Virtual void ShowName () { cout << "US Apple " << endl; } }; // Japanese apple class JapaneseApple: public AbstractApple { public : Virtual void ShowName () { cout << " Japanese apple " << endl; } }; // abstract banana class AbstractBanana { public : Virtual void ShowName () = 0 ; }; // Chinese banana class ChineseBanana: public AbstractBanana { public: Virtual void ShowName () { cout << " Chinese banana " << endl; } }; // American bananas class USABanana: public AbstractBanana { public : Virtual void ShowName () { cout << " American banana " << endl; } }; // Japanese banana class JapaneseBanana: public AbstractBanana { public : Virtual void ShowName () { COUT << " Japanese banana " << endl; } }; // abstract factory for product family class AbstractFactory { public : Virtual AbstractApple CreateApple * () = 0 ; Virtual AbstractBanana CreateBanana * () = 0 ; Virtual AbstractPear CreatePear * () = 0 ; }; // China factory class ChineseFactory: public AbstracFactory { public : Virtual AbstractApple * CreateApple () { return new new ChineseApple; } virtual AbstractBanana* CreateBanana(){ return new ChineseBanana; } virtual AbstractPear* CreatePear(){ return new ChinesePear; } }; //美国工厂 class USAFactory:public AbstracFactory{ public: virtual AbstractApple* CreateApple() { return new USAApple; } virtual AbstractBanana* CreateBanana(){ return new USABanana; } virtual AbstractPear* CreatePear(){ return new USAPear; } }; //日本工厂 class JapaneseFactory:public AbstracFactory{ public: virtual AbstractApple* CreateApple() { return new JapaneseApple; } virtual AbstractBanana* CreateBanana(){ return new JapaneseBanana; } virtual AbstractPear* CreatePear(){ return new JapanesePear; } }; test(){ AbstractFactory* factory = NULL; AbstractApple* apple = NULL; AbstractBanana* banana = NULL: AbstractPear* pear = NULL; //中国工厂 factory = new ChineseFactory; apple = factory->CreateApple(); banana = factory->CreateBanana(); pear = factory->CreatePear(); }