Java Design pattern Builder mode (Builder mode) Introduction _java

Source: Internet
Author: User
Tags memory usage

Builder pattern Definition: Separates the construction of a complex object from its representation, allowing the same build process to create different representations.

The builder pattern is a step-by-step way to create a complex object that allows users to build them only by specifying the type and content of complex objects. The user does not know the specific build details inside. The builder model is very similar to the abstract factory model, and the subtle differences may only be realized in repeated use.

Why Use Builder Mode

is to decouple the process of building a complex object and its parts. Note: Decoupling processes and components.

Because of a complex object, not only are there 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 far more than that, how to assemble these parts into a car, the assembly process is also very complex (need good assembly technology), The builder model is designed to separate parts from the assembly process.

How to Use builder mode

First of all, it is assumed that a complex object is composed of multiple parts, and the builder pattern is to separate the creation of complex objects from the creation of parts, respectively, with builder classes and director classes.

First, you need an interface that defines how to create various parts of a complex object:

Copy Code code as follows:

Public interface Builder {
Create a part A for example to create a car wheel
void Buildparta ();
Create a part B for example to create a car steering wheel
void Buildpartb ();
Create a part C For example to create a car engine
void Buildpartc ();
Return final assembly finished result (return final assembled car)
The assembly process for the finished product is not performed here, but is transferred to the Director class below.
To realize the decoupling process and parts
Product GetResult ();
}

The last complex object is built with director, and the builder interface above encapsulates how to create a widget (complex objects are made up of these parts), that is, how the director content is assembled into the final product:

Copy Code code as follows:

public class Director {
Private Builder Builder;
Public Director (Builder Builder) {
This.builder = Builder;
}
Parta the part to the PARTB PARTC final composition 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 realization of builder ConcreteBuilder:

1. To build or assemble the parts of the product through the concrete interface builder;
2. Define and clarify what specific things it is to create;
3. Provides an interface that can be retrieved from the product.

Copy Code code as follows:

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 final assembly finished result
};
}

Complex objects: Products Product:

Copy Code code as follows:

Public interface Product {}

Parts of Complex objects:
Copy Code code as follows:

Public interface Part {}

Let's see how to invoke the builder pattern:
Copy Code code as follows:

ConcreteBuilder builder = new ConcreteBuilder ();
Director Director = new Director (builder);
Director.construct ();
Product Product = Builder.getresult ();

Application of builder Model

In Java practical use, we often use the concept of "pool", when the resource provider is unable to provide sufficient resources, and these resources need to be repeatedly shared by many users, you need to use the pool.

"Pool" is actually a piece of memory, when there are some complex resources in the pool of "amputation" (such as the database connection pool, perhaps sometimes a connection will be interrupted), if the recycling of these "severed limbs", will improve the efficiency of memory usage, improve the performance of the pool. Modify the Director class in the builder mode to diagnose which part the "severed limb" is broken, and then repair the part.

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.