Template Method mode vs builder Mode

Source: Internet
Author: User

Today, we will discuss two other design patterns: Template Method mode and builder mode. First, let's take a look at the template method mode definition: defines the skeleton of an algorithm in an operation, and delays some steps into the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm. Its UML diagram is as follows:

Abstractclass is an abstract class, which is actually an abstract template. It provides a skeleton of top-level logic, and the logical composition steps are postponed to the concreteclass subclass in the corresponding abstract operations. What are the main features of the template method model? The template method mode is to remove the unchanged behavior to the parent class (superclass) and remove the repeated code in the class to reflect its advantages. The template method mode provides a platform for code reuse. When a process composed of a series of steps needs to be executed, the process is the same at a high level, but some steps may not be implemented at the same time, you can consider using the template method mode. That is to say, when the same and variable behavior are mixed in the implementation of the method subclass, the same behavior will be repeated in the subclass. You can use the template method mode to move these actions to a single place, which helps the subclass to get rid of the entanglement of repeated unchanged behaviors. The following code is a small example: # include <iostream> using namespace STD; Class abstract2 {public: Virtual ~ Abstract2 () {} void templatemethod () {__ methoda () ;__ methodb () ;__ methodc () ;}private: void _ methoda () {cout <"1. Select '" <_ answera () <"'" <Endl;} void _ methodb () {cout <"2. Select '" <_ answerb () <"'" <Endl;} void _ methodc () {cout <"3. Select '" <_ answerc () <"'" <Endl;} protected: abstract2 () {} virtual string _ answera () = 0; virtual string _ answerb () = 0; virtual string _ answerc () = 0 ;}; class concretea2: Public effecact2 {protected: virt Ual string _ answera () {return "A";} virtual string _ answerb () {return "B";} virtual string _ answerc () {return "C" ;}}; class concreteb2: Public effecact2 {protected: Virtual string _ answera () {return "B" ;}virtual string _ answerb () {return "B" ;}virtual string _ answerc () {return "A" ;}}; int main () {abstract2 * pA (New concretea2 ); pa-> templatemethod (); Delete Pa; Pa = new concreteb2; pa-> templatemethod (); System ("Paus E "); Return 0;} Let's look at the builder mode, which is defined as: separating the construction of a complex object from its representation, this allows you to create different representations for the same build process. Its UML diagram is as follows:

Builder is the abstract interface specified for creating each part of a product object. concretebuilder is a specific builder that implements the builder interface and constructs and assembles each part. Director is the conductor who builds a control building process and uses it to isolate the association between users and the building process. So what are the characteristics of it? The builder mode separates the construction of a complex object from its representation, so that different representations can be created during the same construction process. It allows users to get them by specifying the types they need to build, and the specific construction process and details do not need to be known. The benefit of the builder mode is that the building code is separated from the representation code. Because the builder hides how the product is assembled, the base needs to change the internal representation of a product, you only need to define a specific conductor. 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. The following is a small example: # 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 ;}at a glance, you may think there is nothing the same between the two modes, what are their differences and similarities? Some of my opinions are as follows. If the design mode is incorrect, I would like to point it out. First, the template method mode is the behavior mode, while the builder mode is the creation mode. The Design of behavior patterns to the assignment of duties between algorithms and objects not only describe the patterns of objects or classes, but also describe the communication modes between them, and describe the complex control flow that is difficult to trace during runtime, they shift your attention from the control flow to the relationship between objects. The behavior-type class mode uses the Inheritance Mechanism to distribute behavior among classes, such as template method and interpreter. The behavior object mode uses Object combination instead of inheritance. Some behavior object patterns describe how a group of peer-to-peer objects collaborate to complete tasks that cannot be completed by any of the objects, such as mediator, chain
Of responsibility and strategy; other behavior object patterns often encapsulate behavior in an object and assign requests to it.
The Creation Mode abstracts the instantiation process. They help a system to create, combine, and represent its objects independently. A class creation mode uses inheritance to change the class to be instantiated. In the creation mode, an object is instantiated and delegated to another object.
That is to say, the template method mode is used to control the communication between objects and classes, while the builder mode is used to create objects.
Are they actually not in touch? I personally think it is not.
In my opinion, the builder mode also reflects the template method, because the concretebuilder defined in the builder class must be rewritten or the required method is that the builder has defined the required method, these methods can be implemented in the builder class, or they can be postponed to the subclass implementation. Using the preceding two examples, the templatemethod, an important function of the abstract2 class in the template method, is similar to the function build in the ctor class in the builder mode, and is the skeleton that defines the execution of the method. So what are the differences between the pattern of the template method and the pattern of the builder mode and the pattern of the Creation Mode? In my opinion, at the skeleton level of the implementation method, the builder mode uses the combination method, while the template method mode uses the inheritance method. We all know that the combination is better than the inheritance method, so I think the builder is more flexible and can also avoid various problems caused by inheritance, such as code inflation, excessive liability, and difficulty in maintenance. PS: I personally think that the conductor class is the core and soul of the construction model. If there is no conductor class, the builder mode can be degraded into a mode and method mode. For details, I can see another blog: role of the conductor class in builder mode: http://blog.csdn.net/ljianhui/article/details/8280594;

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.