Builder mode, construction mode

Source: Internet
Author: User

Builder mode, construction mode

  • Definition

The builder mode creates all the parts one by one, and thus creates a complete product object. The builder mode hides the product structure and product part construction process from the client, and separates the responsibility for directing the construction process from the responsibility of specific builder parts, achieve responsibility division and encapsulation.

Class diagram:

 

  • Four Elements
  • Director:It is generally a complex object, that is, the process of creating an object is complicated, and there is usually a large amount of code. In this class diagram, the director class is a specific class, not an abstract class. In actual programming, a product class can be composed of different implementations of an abstract class or multiple abstract classes and their implementations.
  • Abstract builder:The purpose of introducing the abstract builder is to hand over the specific construction process to its subclass for implementation. This makes expansion easier. Generally, there are at least two abstract methods, one for building a product, and the other for returning a product.
  • Builder:To implement all the unimplemented methods of abstract classes, there are generally two tasks: creating a product, and returning the constructed product.
  • Product Type:It is generally a complex object, that is, the process of creating an object is complicated, and there is usually a large amount of code. In this class diagram, the product class is a specific class, not an abstract class. In actual programming, a product class can be composed of different implementations of an abstract class or multiple abstract classes and their implementations.
  • Code Implementation

  Product

1 class Product {2 private String name; 3 private String type; 4 public void showProduct () {5 System. out. println ("name:" + name); 6 System. out. println ("model:" + type); 7} 8 public void setName (String name) {9 this. name = name; 10} 11 public void setType (String type) {12 this. type = type; 13} 14}

Abstract Builder

1 abstract class Builder {  2     public abstract void createPart(String arg1, String arg2);  3     public abstract Product getProduct();  4 }

Specific Builder

 1 class ConcreteBuilder extends Builder {   2     private Product product = new Product();   3        4     public Product getProduct() {   5         return product;   6     }   7    8     public void createPart(String arg1, String arg2) {   9         product.setName(arg1);  10         product.setType(arg2);  11     }  12 } 

Director

1 public class Director {2 private Builder builder = new ConcreteBuilder (); 3 public Product getAProduct () {4 builder. createPart ("BMW", "X7"); 5 return builder. getProduct (); 6} 7 public Product getBProduct () {8 builder. createPart ("Audi car", "Q5"); 9 return builder. getProduct (); 10} 11}

The above is only the most basic usage of the builder mode. In real life, there are many deformation modes of the builder mode. If the product consists of multiple parts, you must first create product parts, when using specific product parts to create products, you need to first create parts in the builder and then assemble the products. The parts of each product are not necessarily the same, and a specific builder is created for each product. And so on. When will the builder mode be used:

  • Application scenarios of builder Mode

  1. the object to be generated has a complex internal structure. Each internal component can be an object or only an integral part of an object.

2. The attributes of the objects to be generated are mutually dependent. The builder mode enforces a step-by-step construction process. Therefore, if one property of a product object must be assigned a value after another property is assigned a value, it can be assigned a value.

3. Other objects in the system are used during object creation. These objects are not easy to obtain during product creation.

  • Advantages of builder Mode

  First, the builder mode is well encapsulated. The builder mode can effectively encapsulate changes. In the scenario where the builder mode is used, the product class and the builder class are relatively stable. Therefore, encapsulating the main business logic in the director class can achieve better stability on the whole.

Second, the builder mode is easy to expand. If you have new requirements, you can implement a new builder class. Basically, you do not need to modify the code that has been tested before, so there is no risk to the original function.

  • Difference between builder mode and factory Mode

  The factory mode has just been introduced. They are both creation modes. We can see that the builder model is very similar to the factory model. In general, the builder model only has one "director" role more than the factory model. In the class diagram of the builder mode, if the director class is regarded as the client for the final call, the rest of the diagram can be regarded as a simple factory model.

The builder mode is generally used to createMore complex objectsBecause the object creation process is more complex, the object creation process is independent to form a new class-the director class. In other words, the factory mode encapsulates all the object creation processes in the factory class, and the factory class provides the final product to the client. In the builder mode, the builder class generally only provides the construction of each component in the product class, and delivers the specific construction process to the director class. The Director is responsible for forming various components into products according to specific rules, and then delivering the group-created products to the client.

Therefore, in the process of creating a complex object, there are generally both the builder mode and the factory mode, and different parts are returned using the factory mode, assembling different parts into more complex products using the builder model. The factory model is on a more specific scale, while the builder model is on a more macro scale.

  

Related Article

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.