Design Mode Builder

Source: Internet
Author: User

Builder mode definition:
Divides the structure of a batch object into its representation, so that the same structure can have different representations.

The builder mode is used to create a complex object step by step. It allows users to construct an object only by specifying the class type and content of the complex object. Create an internal component with the internal component unknown. The builder mode is very similar to the abstract worker mode. The worker micro-zone can only be used in anti-bot.

Why?
It is used to create a readable object.ProcessAnd itsPartsDecoupling. Note: decouplingProcessAndParts.

For a complex object, there are not only a large number of components, such as a car, but also many components: a car has a variety of small parts, there are many parts, but more than that, how to configure these parts into a car, this configuration process is also very difficult (requires a good combination of technical skills), the builder mode is to split parts and the composition process.

How to use it?
First, the hypothetical component object is composed of multiple parts. In the builder mode, the component creation and component creation are separated, the difference is represented by builder and director.

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

Public interface builder {

// Create part A, such as creating a car
Void buildparta ();
// Create part B, for example, in the direction of the vehicle
Void buildpartb ();
// Construct part C, such as creating a car Generator
Void buildpartc ();

// Return the final result of the finished component (return the final finished automobile)
// The finished component process is not implemented here, but is moved to the ctor class below.
// Real-time decouplingProcessAndParts
Product getresult ();

}

Using ctor to create the final component object, and in the builder interface above, how does one construct a component (the component object is composed of these parts ), that is to say, how does the Director content organize components into finished products in the final form:

Public class director {

Private Builder;

Public Director (Builder ){
This. Builder = builder;
}
// Combine the part parta partb partc into a hidden object
// This is the process of turning a car into a car in the direction of the car and the engine.
Public void construct (){
Builder. buildparta ();
Builder. buildpartb ();
Builder. buildpartc ();

}

}

Concretebuilder:
Build or configure parts of the product through the user interface builder;
Define and determine what kind of things it wants to create;
Provides an interface that allows you to rebuild the product:

Public class concretebuilder implements builder {

Part parta, partb, partc;
Public void buildparta (){
// This example shows how to build the parta proxy.

};
Public void buildpartb (){
// This example shows how to create a partb proxy.
};
Public void buildpartc (){
// This example shows how to create a partb proxy.
};
Public Product getresult (){
// Return the final result of the finished component
};

}

Product:

Public interface product {}

Parts of a component:

Public interface part {}


Let's take a look at how to call the builder mode:
Concretebuilder builder = new concretebuilder ();
Director dire= new director (builder );

Director. Construct ();
Product = builder. getresult ();

Application of Bridger Mode
In Java practice, we often use the concept of "pool". When the resource source provider cannot provide sufficient resources, and when these resources need to be shared by many users, they need to use the pool.

The "pool" is actually a piece of memory. When there are some artificial Resources in the pool, the "limbs" (such as the memory pool of the resource, it may also be a temporary meeting). If you use these "limbs" in a circle, it will improve the memory usage efficiency and the pool performance. Modify the director class in builder mode to specify the component on which the "" component is located, and then repair the component.

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.