Design Pattern (3)-builder pattern (builder)

Source: Internet
Author: User

The builder mode also belongs to the creation mode.

As the name suggests, a builder is the person who implements a product (Building). After a series of unified operations, they can complete a work and what kind of product is inseparable from the builder. For example, the old Li family now has a land that can be used for building land. It can be used to build a supermarket or a villa. Both old Li's sons are architects, so he called the two sons and asked them to design a complete house construction plan from laying the foundation to the decoration, so that they could see who they wanted to use.

The product designed by Lao Li's son:

The Code is as follows:

Class cbuildingbase {public: Virtual void Foundation () = 0; // ground-based virtual void struction () = 0; // house structure virtual void fitment () = 0; // decoration}; Class cbuildinglee1: Public cbuildingbase {public: Virtual void Foundation () {cout <"Cement Foundation" <Endl;} virtual void struction () {cout <"Villa" <Endl;} virtual void fitment () {cout <"European style \ n" <Endl ;}}; class cbuildinglee2: public cbuildingbase {public: Virtual void Foundation () {cout <"Reinforced Concrete Foundation" <Endl;} virtual void struction () {cout <"commercial housing" <Endl;} virtual void fitment () {cout <" \ n" <Endl ;}};

When Lao Li wants to build a house, he only needs to rely on the abstract construction blueprint. In actual construction, he can use different instances to build the house he wants. This is also the reason why he lives as builder.

// Decision maker -- Lao Li class cdirectorlee {public: cdirectorlee () {m_pbuild = NULL;} void setbuild (cbuildingbase * pbulider) {If (pbulider = NULL) {return ;} m_pbuild = pbulider;} void construct () {m_pbuild-> Foundation (); m_pbuild-> struction (); m_pbuild-> fitment ();}~ Cdirectorlee () {}; PRIVATE: cbuildingbase * m_pbuild ;};

Output:

The builder mode is usually used for a complex class. to decouple it, we divide it into different modules, you can build a general overall (house) through the same interface (Foundation-> house building-> decoration ). Of course, the example is relatively simple. In actual design, we may input parameters to express what kind of bricks are needed to achieve actual production results.

 

The builder mode and the abstractfactory mode have similar functions, because they are used to create large and complex objects. The difference between them is that the builder mode emphasizes thatCreate objects step by stepAnd passSameDifferent result objects can be obtained during the creation process. Generally, objects in builder mode are not directly returned. In the abstractfactory mode, objects are directly returned. The abstractfactory mode emphasizes that the same interface is provided for creating multiple mutually dependent objects.

Reprinted please indicate from: http://blog.csdn.net/zerolxl

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.