Role of the conductor class in builder Mode

Source: Internet
Author: User
When learning the builder model, there is always a problem that cannot be understood, that is, why there is a conductor class. I feel that no conductor class is just as simple and quick, and even easier to understand than no conductor class. The following is an example program written in C ++: // builder. CPP // use the builder mode # include <iostream> using namespace STD; Class automobile // abstract car class {public: Virtual ~ Automobile () {cout <"destruction automobile" <Endl;} virtual void buildwheel () = 0; // build the wheel virtual void buildbody () = 0; // construct the body virtual void buildvehicle () = 0; // construct the car shell protected: automobile () {}}; class car: Public automobile {public: Virtual void buildwheel () {cout <"build the wheel of car" <Endl;} virtual void buildbody () {cout <"build the body of car" <Endl ;} virtual void buildvehicle () {cout <"build the vechicle Car "<Endl ;}~ Car () {cout <"destruction car" <Endl ;}}; class truck: Public automobile {public: Virtual void buildwheel () {cout <"build the wheel of truck" <Endl;} virtual void buildbody () {cout <"build the body of truck" <Endl ;} virtual void buildvehicle () {cout <"build the vechicle of truck" <Endl ;}~ Truck () {cout <"destruction truck" <Endl ;}}; class ctor // The conductor class. The production process of the conductor is {public: ctor (automobile * P ): pam (p) {} void build () {Pam-> buildbody (); Pam-> buildwheel (); Pam-> buildvehicle ();} PRIVATE: automobile * PAM;}; int main () {truck * PTK (new truck); Director D (PTK); D. build (); Delete PTK; System ("pause"); Return 0 ;}the following is an implementation of the conductor class: // test. CPP # include <iostream> using namespace STD; Class automobile {public: Virtual ~ Automobile () {cout <"destruction automobile" <Endl;} virtual void buildwheel () = 0; virtual void buildbody () = 0; virtual void buildvehicle () = 0; void build () // implement {buildbody (); buildwheel (); buildvehicle ();} protected: automobile () in the parent class () {}}; class car: Public automobile {public: Virtual void buildwheel () {cout <"build the wheel of car" <Endl;} virtual void buildbody () {cout <"build the body of car" <Endl;} Virtual void buildvehicle () {cout <"build the vechicle of car" <Endl ;}; class truck: Public automobile {public: Virtual void buildwheel () {cout <"build the wheel of truck" <Endl;} virtual void buildbody () {cout <"build the body of truck" <Endl ;} virtual void buildvehicle () {cout <"build the vechicle of truck" <Endl ;}~ Truck () {cout <"destruction truck" <Endl ;}}; int main () {truck * PTK (new truck); PTK-> build (); delete PTK;} system ("pause"); Return 0;} There is nothing to do with this. Why do we need the conductor class? After thinking about it... the following is my point of thinking. I hope to point out something wrong with the design model of my younger brother. 1. If there are many sub-classes, for example, there are 1000 sub-classes, if there is no conductor class, there may be a problem of code inflation, because test. in CPP, the automobile build function contains 1001 pieces of code. 2. When you need to change the internal representation of a product, if you do not need the conductor class, you need to change the build function in the automobile class, which violates the open-closed principle. To use the builder mode of the conductor class, you only need to define a specific builder. What's more, if the production process of a product is not the only one, such as the rapid development of technology in the future, the car does not need a wheel, but some people like a wheel car or a wheel car, you only need to define one more command class. If you do not need the command class, you need to add another build function in the automobile class. Flexibility is greatly reduced. Let's take a look at the expression of the builder mode: separates the construction of a complex object from its representation, so that different representations can be created in the same build process. It is mainly used to create complex objects. The building sequence between these objects is usually stable, but the building inside the object is usually subject to complex changes. With the builder mode, users only need to specify the types to be built to get them, and the specific process and details do not need to be known. A very important class in the builder is the conductor class, which is used to control the construction process and to isolate the association between users and the construction process. I personally think that the conductor class is the core and soul of the construction model. From the above example, if the conductor class is missing, it cannot be regarded as the builder mode, it can only be said that an interface or abstract class is defined, specifying which functions must be implemented (if the template method mode is an action mode, and the builder mode is a creation mode, the above test. CPP implements a template method mode ). The conductor class not only points out the construction sequence of objects, but also separates users from the construction process and provides a flexible solution for possible changes, so that changes in the construction order or demand will not cause major changes or change the structure of the program.

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.