Design Pattern (4) builder Pattern

Source: Internet
Author: User
The builder mode is the most complex creation mode. It is used to create a complex object that contains multiple components and returns a complete product object to the user, the builder mode focuses on how this complex object is created step by step. for users, there is no need to know the creation process and internal composition details, you only need to create a complete object to make a complex object equivalent to a car to be built, and the property of the object is equivalent to the parts of the car. The process of building a product is equivalent to the process of assembling parts. Because the assembly process is complex, the assembly process of these components is "Externalized" into an object called a builder. The Builder returns the client as a complete product object, users do not need to care about the attributes contained in the object and their assembly methods. This is the pattern motivation of the builder mode. Separates the construction of a complex object from its representation so that different representations can be created in the same construction process. The Builder mode creates a complex object step by step, it allows users to create complex objects only through the types and content of complex objects. users do not need to know the details of internal creation. Case study: Description: KFC creates different packages. A package is a complex object. It generally consists of a staple food (hamburger, chicken roll) and a beverage (fruit juice, Cola, different packages have different components, and KFC waiters can assemble these components step by step based on different customer needs to construct a complete package and then give it to the customer. Here, the waiter is equivalent to the command role in the construction mode, calling different builders to complete the assembly process, and then sending the product to the user Code : // Product meal (Package). This is a complex product object. It includes two member attributes: food and dirnk. Food indicates staple food, and drink indicates drinks, there is also the getter setter method public class meal {private string food; private string drink; Public void setfood ($ food) {$ this-> food = $ food ;} public void setdrink ($ dirnk) {$ this-> dirnk = $ dirnk;} public void getfood () {return $ this-> food;} public void getdrink () {return $ this-> dirnk; }}// mealbuilder class (package builder class), which is an abstract class that lives the abstract component Assembler Methods buildfood () and builddrink (), meanbuilder abstract class also defines the meal type object meal, the factory method getmeal is provided to return the meal object public abstract class mealbuilder {protected $ meal = new meal (); public abstract void builderfood (); public abstract void builderdrink (); Public meal getmeal () {return $ this-> meal ;}/// submealbuildera, the creator of Package A, which is a subclass of the abstract class mealbuilder, implements the component assembly method of builderfood and builderdrink declared in the abstract parent class. This package has a hamburger and a muscle volume to form a public class sub Mealbuildera extends mealbuilder {public void builderfood () {$ this-> meal-> setfood ('one hamburger ');} public void builderdrink () {$ this-> meal-> setdrink ('one cola ') ;}}// submealbuilderb for the specific builder class, used to create Package B, consists of a chicken roll and a cup of juice. Public class submealbuilderb extends mealbuilder {public void builderfood () {$ this-> meal-> setfood ('one chicken roll ');} public void builderdrink () {$ this-> meal-> setdrink ('one juiced juice ') ;}}// conductor kfcwaiter (KFC In the KFC package production project, this is the KFC waiter. In this project, the variable MB of the abstract builder type is defined. The specific construction type is determined by the client, in its construct () method, call the component assembly method of the MB object and the factory method that returns the meal object to return a complete package containing the staple food and drinks to the client. Public class kfcwailter {private mealbuilder $ MB; Public void setmealbuilder (mealbuilder $ MB) {$ this-> MB = $ MB;} Public meal construct () {$ this-> MB-> builderfood (); $ this-> MB-> builderdrink (); return $ this-> MB-> getmeal ();}} // The client call code mealbuilder $ MB = new config: Get ('use _ builder'); // obtain the specific builder class and instantiate kfcwailter $ waiter = new kfcwailter (); // instantiate the caller class $ wailter-> setmealbuilder ($ MB); // set the specific builder class meal for the conductor $ Meal = $ wailter-> constrcunt (); // assemble and return the final summary: 1. the builder mode separates the representation of a complex object, so that different representations can be created during the same creation process. The builder mode is used to create a complex object step by step. It allows users to create complex objects only by specifying the types and content of complex objects. users do not need to know the specific internal building details, the builder mode belongs to the class creation mode. 2. the builder mode contains the following four roles: the abstract builder creates a lottery interface for each part of a product object for a specific builder, which is the parent class of the specific builder. The specific builder inherits the abstract builder, implement the construction and assembly methods of each component in the parent class, and define the complex object it creates. The product choice is the complex object constructed by the drug, which contains multiple parts; the conductor is responsible for arranging the construction order of complex objects. The conductor is associated with the lottery builder and directs abstract-oriented programming to construct complex objects by calling specific builder methods. 3. A conductor class is introduced in the builder mode, which has two main roles: the first is to isolate the process of customer and production, and the second is the process of creating complex products. The conductor only needs to program the lottery. The client only needs to know the type of the complex object to be constructed, and can call the related methods of the creator through the conductor class to build the product. 4. the main advantage of the builder mode is that the client does not need to know the specific internal creation process of the product and decouples the product itself from the creation process. Different products can be created in the same creation process, each specific builder is independent of each other and has nothing to do with other builders. Therefore, it is convenient to replace the builder or add a specific builder in the system to complete the demand change, in line with the open and closed principles, the product creation process can also be controlled more precisely. The main drawback is that the products created by the builder mode generally have many commonalities, and their components are similar, therefore, the scope of use is limited to a certain extent. If the internal changes of the product are too complex, many builder classes may need to be defined to implement this change, as a result, the system becomes bulky and bloated, which is not conducive to maintenance. 5. builder mode usage: The product objects to be generated have complex internal structures. These product objects usually contain multiple Member attributes. The attributes of the product objects to be generated are mutually dependent, you need to specify the generation sequence. The object creation process is independent of the class that creates the object. It isolates the creation and use of complex objects and allows different product objects to be created in the same creation process.

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.