Java Builder pattern (go)

Source: Internet
Author: User

1. Concept

Separating a complex build from its presentation allows the same build process to create different representations. [Construction and presentation separation, different representations of construction]

The difference from the abstract factory: in the builder model, there is a mentor who manages the builder, the user is in contact with the mentor, and the mentor contacts the builder to get the product. That is, the construction model can enforce a process of construction in stages.

  The building pattern is to encapsulate complex internal creation inside, and for external calls, only incoming builders and build tools are needed, and the caller does not need to be concerned about how the interior is built to create the finished product.

A simple example, such as a car, a lot of parts, a wheel, a steering wheel, an engine, a variety of small parts, and so on, a lot of parts, but more than that, how to assemble these parts into a car, the assembly process is complicated (requires good assembly technology), and the builder mode is designed to separate parts from assembly.

2.UML figure

3. Code

 public interface Builder {
void Buildparta ();
void Buildpartb ();
void Buildpartc ();

Product getresult ();
}

//Concrete Build Tool
public class ConcreteBuilder implements Builder {
Part parta, PartB, PARTC;

public void Buildparta () {
//Here is the code that specifically constructs Parta
};
public void Buildpartb () {
//Here is the code specifically for how to build PARTB
};
public void Buildpartc () {
//Here is the code specifically for how to build PARTB
};
Public Product GetResult () {
//returns the final assembly result
};
}

//builder
public class Director {
Private builder Builder;

Public Director (builder builder) {
This.builder = builder;      
}
public void construct () {
Builder.buildparta ();
BUILDER.BUILDPARTB ();
BUILDER.BUILDPARTC ();
}
}


public interface Product {}
Public interface part {}
    Here's how to call Builder:
ConcreteBuilder builder = new ConcreteBuilder ();

  

Product Product = Builder.getresult ();

4. Application Scenarios

This pattern is javamail used in Java applications.

Java Builder pattern (go)

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.