First, the definition
Abstract Factory: There is an abstract class. There are multiple interfaces in the class to create different objects.
Factory method: There is a class. The class has an interface to create an object.
Prototype: The class has a clone interface to either depth or "half-depth" to clone its own object.
Builder: There are multiple interfaces in a class to manipulate a variety of complex steps. There is also an object that is created by an interface for or through a combination of individual step operations.
Second, some of the application
In the beginning, it was generally created using the factory method. When the requirements are extended later, depending on the situation, the extension method needs to be used.
Abstract Factory:
• A system is independent of the creation, composition, and presentation of its products.
• When a system is to be configured by one of multiple product families.
• When you want to emphasize the design of a series of related product objects for joint use.
• When you provide a Product class library and just want to display their interfaces instead of implementing them.
Factory Method:
• When a class does not know the class of the object it must create.
• When a class wants to specify the object it creates by its subclasses.
• When a class delegates the responsibility of creating an object to one of several helper subclasses, and you want to set which helper subclass is the proxy for this information when it is localized.
Third, partial realization situation
Factory Method:
//Mode 1classCreator { Public: Product* Create (intID) {if(id = =1) return NewProductImpl1 (); returnnullptr; }};classCREATORIMPL1: Publiccreator{ Public: Product* Create (intID) {if(id = =2) return NewProductImpl2 (); return__super::create (ID); }};//Mode 2classCreator { Public: Product* Create () =0;}; Template<typename ProductType >classCreateimpl: PublicCreator { Public: Product*Create () {return NewProductType; }};
View Code
Builder:
classProduct;classBuilder { Public: Virtual voidOpt1 () {std::cout <<"def builder::opt1\n"; } Virtual voidOpt2 () {std::cout <<"def builder::opt2\n"; } Virtualproduct* Create () =0;};classBUILDERIMPL1: PublicBuilder { Public: Virtual voidOpt1 () {std::cout <<"builderimpl1::opt1\n"; } Virtualproduct*Create () {Opt1 (); OPT3 (); Opt2 (); }Private: voidOPT3 () {std::cout <<"builderimpl1::opt3\n" };};
View Code
Iv. Summary
Cond
Gof Abstract Factory, factory method, prototype, and builder