Builder mode of design pattern

Source: Internet
Author: User

builder--Builder Mode: separates the construction and presentation of a complex object so that the same build process can have different representations.

As you can see from the definition, the builder pattern has two important points,

1. Separation of construction and presentation

2. The same build process has different representations.

As you can see here, the build process is the same for the builder mode. And in the construction process, according to different needs. Build a different object.

Builder mode is the object creation pattern, which is a step-by-step creation of a complex object that allows the user to build them only by specifying the type and content of complex objects.

The user does not need to know the specifics of the build in-house (i.e. the build process)

UML diagram for Builder mode:

1. Director: The conductor, based on the customer's prompt, decides which builder to use to build the product

2. Builder: An abstract build class that defines the build process.

3. ConcreteBuilder: Actual build class, build different products

4. Product: The products that need to be built, the specific building class, will be at each step of the time, the part of the product is structured, and finally completed.

Usage Scenarios for builder mode:

When creating complex objects, the order in which the objects are built is stable, but when each part of the object has a different change, the builder mode can be used.

Example:

A very good restaurant, the recent competition, they observe the opponent, found that the opponent to different groups launched a variety of packages, such as set a: Mung bean noodles + red bean soup + turnip Silk set B: Cool Skin + Bingfeng. Package C: Chicken leg rice + cola

After the boss observes, the decision also launches the corresponding package. and use builder mode to improve package generation efficiency

Package = Staple Food + drinks + snacks

1 classWaiter2 {3 Private:4buildtaocan*_taocan;5  Public:6     voidSettaocan (buildtaocan*TC)7     {8          This->_taocan =TC;9     }Ten  One     voidConstruct () A     { -_taocan->Addfood (); -_taocan->Adddrink (); the_taocan->addsnacks (); -     } -  - }; +  - classBuildtaocan + { A     Virtual voidAddfood (); at     Virtual voidAdddrink (); -     Virtual voidaddsnacks (); -     Virtual voidGettaocan (); - }; -  - classBuildtaocana in { - Private: to Taocan _TC; +  Public: -     Virtual    voidGettaocan () the     { *cout <<"Taocan a"<< _tc.food () << _TC. Drink () << _tc.snacks () <<Endl; $     }Panax Notoginseng     Virtual voidAdddrink () -     { theTc.setdrink ("Hongdoutang"); +     } A     Virtual voidAddfood () the     { +Tc.setfood ("Lvdoumian"); -     } $};

Client

Waiter Waiter ();

Buildtaocana Cana;

Waiter.settaocan (&cana);

Waiter. Construct ();

Cana.gettaocan ();

Builder mode of design pattern

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.