Builder mode of Design Pattern

Source: Internet
Author: User

Builder mode of Design Pattern

Builder Pattern: separates the construction of a complex object (product) from its representation so that different representations can be created during the same build process. It is also called the generator mode.
The builder mode is the assembly and creation of complex objects. It is an object creation mode that satisfies the dependency reversal principle (details depend on abstraction rather than abstraction depends on details ).

If a complex object can be viewed as a product, the method (or internal representation) of the object corresponds to the product parts, customers do not need to know the specific construction details. They only need to specify the type to be built to get the corresponding product.
The builder mode mainly includes four roles:

Abstract Builder: Declares the abstract interface specified for creating each part of a product. ConcreteBuilder: Implements abstract interfaces to create and assemble components. Conductor Director: Also called Director, responsible for handling the assembly sequence of complex objects and isolating the production process between the customer and the product. The client only needs to interact with the conductor. Product: a specific Product.

The UML diagram is as follows:

The Code is as follows:

Builder.java
/*** Abstract Builder * @ author sywyg * @ since 2015.8.27 **/public interface Builder {void BuildProduct (); void addPartA (String name); void addPartB (String name ); void addPartC (String name); Product getResult ();}
ConcreteBuilder.java
/*** Specific Builder * @ author sywyg * @ since 2015.8.27 **/public class ConcreteBuilder implements Builder {private Product product Product; @ Override public void BuildProduct () {product = new Product () ;}@ Override public void addPartA (String name) {A a = new A (name); product. setA (a); System. out. println (Part A added successfully);} @ Override public void addPartB (String name) {B B B = new B (name); product. setB (B); System. out. println (part B added successfully);} @ Override public void addPartC (String name) {C c = new C (name); product. setC (c); System. out. println (Part C added successfully) ;}@ Override public Product getResult () {return product ;}}
Director.java
/*** Conductor, directing the product production process * @ author sywyg * @ since 2015.8.27 **/public class Director {private Builder builder; public Director (Builder builder) {this. builder = builder;} public Product construct () {builder. buildProduct (); builder. addPartA (Part A); builder. addPartC (Part C); builder. addPartB (Part B); return builder. getResult ();}}
Product.java
/*** Product class: The Product class consists of three components: * @ author sywyg * @ since 2015.8.27 **/public class Product {private A a; private B B; private C; public A getA () {return a;} public void setA (A a) {this. a = a;} public B getB () {return B;} public void setB (B B) {this. B = B;} public C getC () {return c;} public void setC (C c) {this. c = c ;}} class A {private String name; public A (String name) {this. name = name ;}} class B {private String name; public B (String name) {this. name = name ;}} class C {private String name; public C (String name) {this. name = name ;}}

Test the code above:

Public class Client {public static void main (String [] args) {// first create a specific Builder builder Builder = new ConcreteBuilder (); // create a Director class director ctor = new Director (builder) based on the specific builder; // directly call the director class construction method to create a Product product Product = Director. construct ();}}
The builder's advantages encapsulate the construction process of a complex object, and the details depend on abstraction. Allow objects to be created in multiple steps and change the project. Hide the internal performance of the product to the customer. The product implementation can be replaced (with specific builders) because the customer only sees an abstract interface. Disadvantages compared with the factory model, customers who use the builder mode to create objects need to have more domain knowledge. The products created by the builder mode generally share many similarities. The components of the products are similar and the scope of use is limited. If the internal changes of the product are complex, many specific builder classes need to be defined to implement such changes, resulting in a huge system. The purpose is often used to create a composite structure. The property of the product object to be generated depends on each other, and the order of generation needs to be specified.

 

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.