Java Builder Mode (builder mode)

Source: Internet
Author: User


Builder mode Definition: separates the construction of a complex object from its representation so that the same build process can create different representations.

The builder pattern is a step-by-step creation of a complex object that allows the user to build them only by specifying the type and content of the complex objects. The user is not aware of the specifics of the build internals. The builder pattern is very similar to the abstract factory pattern, with subtle differences that can only be experienced in repeated use. Why the builder pattern is used to decouple the process of building complex objects from its parts.Note: The decoupling process and components.

Because a complex object, not only has a lot of components, such as cars, there are many parts: wheels, Steering wheel, engine, and a variety of small parts and so on, many parts, but much more than these, how to assemble these parts into a car, the assembly process is also very complex (need good assembly technology), The builder mode is designed to separate parts from the assembly process. How to use the builder pattern first, assume that a complex object is made up of multiple parts, and the builder pattern is to separate the creation of complex objects from the creation of parts, represented by the builder class and the Director class, respectively.

First, you need an interface that defines how to create individual parts of a complex object:
Public interface Builder {
Creating a part A such as creating a car wheel
void Buildparta ();
Creating a part B such as creating a car steering wheel
void Buildpartb ();
Creating a part C such as creating a car engine
void Buildpartc ();
Returns the final assembly of the finished product (back to the last assembled car)
The assembly process for the finished product is not carried out here, but instead is transferred to the following Director class.
Thus realizing the understanding of the coupling process and components
Product GetResult ();
}

The final complex object is built with the director, and in the builder interface above it is how to create parts (complex objects are made up of these parts), that is, how the director's content is finally assembled into a finished product:
public class Director {
Private builder Builder;
Public Director (Builder builder) {
This.builder = Builder;
}
Parta a part PartB PARTC the final composition of a complex object
This is the process of assembling the wheel steering wheel and the engine into a car.
public void construct () {
Builder.buildparta ();
BUILDER.BUILDPARTB ();
BUILDER.BUILDPARTC ();
}
}

The concrete implementation of builder ConcreteBuilder:
    • To build or assemble parts of the product by completing the interface builder specifically;
    • Define and clarify what the specific thing it is to create;
    • Provides an interface that can be re-acquired for a product.

public class ConcreteBuilder implements Builder {
Part Parta, PartB, PARTC;
public void Buildparta () {
Here's how to build Parta code specifically
};
public void Buildpartb () {
Here's how to build PARTB code specifically
};
public void Buildpartc () {
Here's how to build PARTB code specifically
};
Public Product GetResult () {
Return to final assembly of finished product results
};
}

Complex objects: Product Products:
Public interface Product {}
Parts of Complex objects:
Public interface Part {}

Let's see how to call the builder pattern:
ConcreteBuilder builder = new ConcreteBuilder ();
Director Director = new Director (builder);
Director.construct ();
Product Product = Builder.getresult (); The application of builder mode in Java is used, we often use the concept of "pool", when the resource provider can not provide enough resources, and these resources need to be shared repeatedly by many users, it is necessary to use the pool.

"Pool" is actually a piece of memory, when there are some complex resources in the pool "limb" (such as the database connection pool, perhaps sometimes a connection will be interrupted), if the recycling of these "limbs", will improve the efficiency of memory use, improve the performance of the pool. Modify the Director class in the builder mode so that it can diagnose which part the "limb" is broken on, and then fix the part.
Builder mode Overview
    Separating the construction of a complex object from its representation allows the same build process to create different representations.
Applicability
    1. When creating complex objects, the algorithm should be independent of the parts of the object and how they are assembled.    2. When the construction process must allow the constructed object to have different representations.
participants
    1.Builder      specifies an abstract interface for each part that creates a product object. The    2.ConcreteBuilder      implements the builder interface to construct and assemble individual parts of the product.      defines and clarifies the representation that it creates.      provides an interface for retrieving products.    3.Director      Constructs an object that uses the builder interface.    4.Product      represents the complex object being constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process.      contains the classes that define the components that comprise the assembly, including the interfaces that assemble the parts into the final product.
class Diagram Example Builder
Public interface Personbuilder {    void Buildhead ();        void Buildbody ();        void Buildfoot ();    Person Buildperson ();}
ConcreteBuilder
public class Manbuilder implements Personbuilder {person person    ;        Public Manbuilder () {Person        = new Man ();    }        public void Buildbody () {        person.setbody ("Build Man's Body");    public void Buildfoot () {        person.setfoot ("Build Man's Feet");    }    public void Buildhead () {        person.sethead ("Build Man's Head");    }    Public Person Buildperson () {        return person;    }}
Director
public class Persondirector {    Constructperson (Personbuilder pb) {        pb.buildhead ();        Pb.buildbody ();        Pb.buildfoot ();        return Pb.buildperson ();    }}
Product
public class Person {    private String head;        Private String body;        Private String foot;    Public String GetHead () {        return head;    }    public void Sethead (String head) {        this.head = head;    }    Public String GetBody () {        return body;    }    public void Setbody (String body) {        this.body = body;    }    Public String Getfoot () {        return foot;    }    public void Setfoot (String foot) {        this.foot = foot;    }}
public class Mans extends person {}
Test
public class test{public        static void Main (string[] args) {        persondirector pd = new Persondirector ();        Person person = Pd.constructperson (new Manbuilder ());        System.out.println (Person.getbody ());        System.out.println (Person.getfoot ());        System.out.println (Person.gethead ());}    }
result
Build men's bodies build men's feet build men's heads


Java Builder Mode (builder mode)

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.