Design Pattern Learning Notes (12)-builder Builder model

Source: Internet
Author: User

Builder Mode Definition:

The construction of a complex object is separated from its representation, allowing the same build process to create different representations.

When I first came into contact with this model, I couldn't understand what it meant. So, the internet Google a lap, and finally get this universally accepted explanation:

The build pattern is a step-by-step creation of a complex object that allows users to build them only by specifying the type and content of complex objects, and the user does not know the specifics of the build inside.

Here is an example to illustrate the use of this pattern, the code is as follows:

Import java.util.ArrayList;

Interface builder{
public void Buildparta ();
public void Buildpartb ();
public void Buildpartc ();
Public Product getproduct ();
}
Class product{
Private arraylist<string> parts=new arraylist<string> ();
public void Add (String part) {
Parts.add (part);
}
public void Show () {
SYSTEM.OUT.PRINTLN ("product has the following components:");
for (String s:parts) {
System.out.println (s);
}
}
}

Class Worker implements builder{
Private product product;

public void Buildparta () {
Product=new Product ();
Product.add ("Part A");

}

public void Buildpartb () {
Product.add ("Part B");

}

public void Buildpartc () {
Product.add ("Part C");

}
Public Product getproduct () {
return product;
}
}
Class designer{
public void Order (Builder Builder) {
Builder.buildparta ();
BUILDER.BUILDPARTB ();
BUILDER.BUILDPARTC ();
}
}
public class Test {

public static void Main (string[] args) {
Designer designer=new Designer ();
Builder builder=new Worker ();
Designer.order (builder);
Product product =builder.getproduct ();
Product.show ();

}
}

The output results are as follows:

Product consists of the following parts:

Part A

Part B

Section C

From this example we can see that the builder model is a part of the process of building objects.

Summary: The builder model is primarily designed to decouple the process of building a complex object and its components. So that we don't have to care about how each part is assembled.

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.