Creator mode (builder)

Source: Internet
Author: User
Tags map class

In design patterns, the creator (builder) mode is defined to separate the construction of a complex object from its representation, so that the same construction process can create different representations. I believe most beginners do not understand this sentence.

 

In fact, the Creator mode is to create a complex object step by step. It allows users to build complex objects by specifying the types and content of complex objects. users do not know the specific internal building details. The builder mode is very similar to the simple factory mode. The minor differences will be mentioned later.

 

Because a complex object not only has many components, such as a car, but also many components: wheels, steering wheel, engine, and various small parts. There are many components, but far more than these. How to assemble these components into an automobile is also complicated (requires good assembly technology). The Builder mode is to separate the components from the assembly process. This is the separation of construction and representation. Some books also say that the Creator mode is to decouple the process of building complex objects from its components. Note: It is the decoupling process and component. The essence of the Creator mode is to separate the construction of a complex object from its representation, so that different representations can be created during the same construction process.

 

Assume that a complex object is composed of multiple parts. The Builder mode separates the creation of complex objects and the creation of parts, and uses the builder interface to represent component creation, director class to create an object.

 

First, you need an interface that defines how to create components of a complex object:
Public interface builder {

// Create part A, for example, creating a Car Wheel
Void buildparta ();
// Create part B, for example, create a car steering wheel
Void buildpartb ();
// Create part C, for example, create a car engine
Void buildpartc ();
// Create part D, for example, create a car window
Void buildpartd ();

// Return the final assembly result (return the final assembled car)
// The assembly process of finished products is not carried out here, but transferred to the director class below.
// Achieves the decoupling process and components
Product getresult ();
}

 

The concretebuilder class of the builder interface is used to construct or assemble components of a product. It defines and clarifies the specific things it wants to create. It provides an interface for obtaining products again:
Package map_package;

Public interface map_interface {

Public void create_weather ();

Public void create_house ();

Public void create_tree ();

Public void create_way ();
}

 

Public class concretebuilder implements builder {

Part parta, partb, partc;
Public void buildparta (){
// Todo: here is how to build the parta code
};
Public void buildpartb (){
// Todo: here is how to build the partb code
};
Public void buildpartc (){
// Todo: here is how to build the partc code
};
Public void buildpartd (){
// Todo: Here is the code for constructing partd.
};
Public Product getresult (){
// Return the final assembly result
};

}

 

Finally, we use ctor to construct the final complex object, and the builder interface above encapsulates how to create parts (complex objects are composed of these parts ), that is to say, the content of director is how to finally assemble components into finished products:
Public class director {
Private Builder;

Public Director (Builder ){
This. Builder = builder;
}
// Combine parts such as parta partb partc to form a complex object
// Here is the process of assembling the wheel and engine into a car,
// Note that the order must be in the order of A-> D-> C-> B.
Public void construct (){
Builder. buildparta ();
Builder. buildpartd ();
Builder. buildpartc ();
Builder. buildpartb ();
}
}

 

Complex object interface: product:

 

Public interface product {...... }

 

Complex objects:

 

Public class car implements product {...... }

 

Component interfaces of complex objects:

 

Public interface part {}

 

Components of complex objects:

 

Public class parta implements Part {...... }

Public class partb implements Part {...... }

Public class partc implements Part {...... }

Public class partd implements Part {...... }

 

Finally, let's look at how to call the builder mode:

Concretebuilder builder = new concretebuilder ();
Director dire= new director (builder );

 

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

 

We can see that the biggest difference with a simple factory is that the Creator mode does not focus on Object creation, but on Object part creation, so we did not find any new keywords in the director class.

 

Next, we will use an example to illustrate the Java code implementation of the builder mode:

Create map interface:
Package map_package;

Public interface map_interface {

Public void create_weather ();

Public void create_house ();

Public void create_tree ();

Public void create_way ();
}

 

Create a sunny map:
Package map_package;

Public class map_sun implements map_interface {

Public void create_weather (){
System. Out. println ("Sunday ");
}

Public void create_house (){
System. Out. println ("glass on the room is shiny ");
}

Public void create_tree (){
System. Out. println ("the color of the tree is light green ");
}

Public void create_way (){
System. Out. println ("road surface is dry ");
}

}

 

Create a cloudy map:
Package map_package;

Public class map_cloudy implements map_interface {

Public void create_weather (){
System. Out. println ("Cloudy ");
}

Public void create_house (){
System. Out. println ("the glass on the room is dark ");
}

Public void create_tree (){
System. Out. println ("the color of the tree is dark green ");
}

Public void create_way (){
System. Out. println ("the road surface is wet ");
}

}

 

Let's take a look at how the builder mode encapsulates the construction process, improves the quality of our code, and creates a new high-definition builder class:
Package map_build;
Import map_package.map_interface;
Public class map_build_adv {

Private map_interface map_interface_ref;

Public map_build_adv (map_interface map_interface_ref ){
Super ();
This. map_interface_ref = map_interface_ref;
}

Public void create_map (){
System. Out. println ("Creating a high-definition map ");
// The order of creation is very important to create from the sky to the road
Map_interface_ref.create_weather ();
Map_interface_ref.create_house ();
Map_interface_ref.create_tree ();
Map_interface_ref.create_way ();

}

}

 

Low-definition builder construction:
Package map_build;
Import map_package.map_interface;
Public class map_build_low {

Private map_interface map_interface_ref;

Public map_build_low (map_interface map_interface_ref ){
Super ();
This. map_interface_ref = map_interface_ref;
}

Public void create_map (){
System. Out. println ("Creating a low-definition map ");
// The order of creation is very important to create from the sky to the road
Map_interface_ref.create_weather ();
Map_interface_ref.create_house ();
// Map_interface_ref.create_tree (); remove the tree creation process
Map_interface_ref.create_way ();

}

}

 

After the work is done, we can use it:
Package run_main;

Import map_build.map_build_adv;
Import map_build.map_build_low;
Import map_package.map_cloudy;
Import map_package.map_sun;

Public class run_main {

Public static void main (string [] ARGs ){

Map_cloudy = new map_cloudy ();
Map_build_adv = new map_build_adv (map_cloudy );
Map_build_adv.create_map ();

System. Out. println ();

Map_sun = new map_sun ();
Map_build_low = new map_build_low (map_sun );
Map_build_low.create_map ();

}
}

 

The program running result is as follows:
Create a high-definition Map
Cloudy
The room is dark with glass
The color of the tree is dark green.
Wet road surface

Create a low-definition Map
Sunny days
Glass on the room is shining
The road surface is dry.

 

From the program, we can see that the builder mode encapsulates the constant creation process, and the creation process is separated from the main method. In this way, the internal creation process is separated from the code of the presentation layer, it facilitates the modification of the creation process function. In addition, we can find that the code design and functions are similar to the facade appearance mode. The difference is that the builder mode aims to get different results through different builders in the same build process, the appearance mode does not require different builders or different results, but simply combines several interfaces into an advanced interface without affecting the original results, the purpose is to make the call easier.

 

There are two creators in this program: the high painter builder and the Low-definition builder. They all encapsulate the map creation process, which is fixed, however, different builder classes can return non-style maps. The Builder specifies the object creation process. For example, the creation process of a high-definition builder is:
Map_interface_ref.create_weather ();
Map_interface_ref.create_house ();
Map_interface_ref.create_tree ();
Map_interface_ref.create_way ();

 

You must execute four methods to create a high-definition map. If you do not use the builder mode, you can directly call the create_xxxx method of the map class. If there are dozens of create_xxxx methods, it is very likely that some of these methods will not be called to affect the effect of the final map. Therefore, we need to use the builder mode to define the map creation process. This is a "code of conduct ".

 

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.