GOF Abstract Factory, factory method, prototype and builder

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.