Design Model learning-Builder

Source: Internet
Author: User

Design Model learning-Builder
What

Builder: separates the construction and representation of a complex object so that different representations can be created during the same building process.

Why

Builder is also a creation mode. It is a step-by-step Wizard to create a complex object. The Builder interface defines parts for creating complex objects, director creates complex objects based on the builder passed in on the client.
Builder is suitable for the creation of complex objects. The creation of this object has stable steps or relatively stable "parts", but "parts" (Steps) internal building is complex and variable.
The Builder in the design patterns book applies to the following situations:
1. When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
2. When the constructor must allow different representations of the object to be constructed.

How

Assume that in the following scenario, an automobile with wheels, steering wheel, engine, and different brands has different algorithms. In this example, the builder mode is used for implementation.
Auto builder Interface

public abstract class CarBuilder {    protected Car car=null;    public abstract void buildWheel();    public abstract void buildEngine();    public abstract void buildSteeringWheel();    public Car getResult(){        return car;    }}

Jeep auto builder implementation

public class JeepCarBuilder extends CarBuilder {    @Override    public void buildWheel() {        System.out.println("construct jeep car wheel");    }    @Override    public void buildEngine() {        System.out.println("construct jeep car engine");    }    @Override    public void buildSteeringWheel() {        System.out.println("construct jeep car steering wheel");    }}

Chery Auto builder implementation

public class CheryCarBuilder extends CarBuilder {    @Override    public void buildWheel() {        System.out.println("construct chery car wheel");    }    @Override    public void buildEngine() {        System.out.println("construct chery car engine");    }    @Override    public void buildSteeringWheel() {        System.out.println("construct chery steering wheel");    }}

Director implementation

public class CarDirector {    private CarBuilder builder;    public CarDirector(CarBuilder builder){        this.builder=builder;    }    public Car construct(){        builder.buildSteeringWheel();        builder.buildEngine();        builder.buildWheel();        return builder.getResult();    }}

Client call

public class App {    public static void main( String[] args ){        CarBuilder builder=new JeepCarBuilder();        CarDirector director=new CarDirector(builder);        director.construct();        CarBuilder builder1=new CheryCarBuilder();        CarDirector director1=new CarDirector(builder1);        director1.construct();    }}

The class diagram in this example is as follows:


Discuss

The advantage of the Builder mode is to separate the Construction Code from the representation code. To add a series of products, you only need to add the corresponding builder interface implementation. If you need to change the product representation, you only need to modify the implementation of the builder interface.
In jdk, StringBuilder is a simple builder mode, where StringBuilder acts as builder and construct, and Client acts as DirectZ compiler? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcqGjPGJyPgrA4M28yOfPwqOowLTX1GhhcHB5aGlwcHk =" s Blog ):

Reference

1. misunderstanding of the Builder mode: Is the Builder mode used to encapsulate the construction of complex objects?

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.