Design Patterns builder mode Yakuu (original)

Source: Internet
Author: User

This article is translated from the original Yakuu. It has been well written and has its own insights. After reading it, I have gained a lot and I like his style ~~

Concept:
Builder: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

Bytes ---------------------------------------------------------------------------------------

"Hey, Andy, come and help me install the device .", Helen shouted again.

"Okay, okay, oh ......, New machine !", As you walked over, andytao looked bright. Alas, seeing the new configurations of others, he has an impulse to take it for himself. To be honest, there are only a few of us playing computer games looking at the mountains...

"This big girl still won't install machines until now. How did you learn it ?" Andytao said so and thought, "You 'd better never learn it !"

"Okay, okay. Thank you! However, it would be nice to have your mind installed ."

"Hey, no, too much ...... Well, let's talk about the design mode !"

"Is this installation a design model ?"

"Listen !"

"Oh ...", Helen's mouth was really TMD cute, and andytao thought about it.

"I will tell you about the builder mode in the design mode. The builder mode is used to create complex objects 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 build details ."

"Hey, isn't this similar to the abstract factory model ?"

"The Builder mode is very similar to the abstract factory mode. The minor differences can be realized only after repeated use. I will talk about some differences between the builder and abstract factory modes later ."

"It is generally used in the following two cases:
1. When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
2. When the constructor must allow different representations of the object to be constructed.
The above statement is too abstract. Simply put, it is used to decouple the process of building a complex object from its components, so as to separate the construction of a complex object from its representation, this allows you to create different representations for the same build process."

"Don't understand ...... What's more ?"

"Because a complex object has a large number of components, and sometimes it gets dizzy, let's talk about PC: PC has many components:

Motherboard, CPU, hard disk, display ...... There are also a variety of small parts, accessories and so on; there are many parts, how to assemble these parts into a PC?

This assembly process is also very complicated and requires a good assembly technology (cannot scare you ?), In order to better organize the large PC object, we need a corresponding method for assembly. The builder model we want to talk about is to separate parts from the assembly process ."

"Don't understand ......"

"Alas, stupid, you can't ......"

"Well, let's talk about how to use it first !"

"Assume that a complex object is composed of multiple parts. The Builder mode separates the creation of complex objects from the creation of parts, which are represented by the builder class and the director class respectively ."

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

Public interface builder {

// Create part A, such as producing the main board
Void buildparta ();
// Create part B, for example, producing CPU
Void buildpartb ();
// Create a part C, such as a hard disk
Void buildpartc ();
......
// Return the final assembly result (return the final assembled PC)
// The assembly process of finished products is not carried out here, but transferred to the director class below.
// Separation of processes and components
Product getproduct ();
}
 

Build the final complex object with ctor, and encapsulate in the above builder interface how to create parts (the complex object is composed

These components), that is, how the director content finally assembles the components into finished products:

Public class director {

Private Builder;

Public Director (Builder ){
This. Builder = builder;
}
// Combine part parta partb partc to form a complex object
// The process of assembling the motherboard, CPU, and hard disk into a PC
Public void construct (){
Builder. buildparta ();
Builder. buildpartb ();
Builder. buildpartc ();
}
}
 

Concretebuilder:
Build or assemble product components through specific interface builder;
Define and clarify what specific things it wants to create;
Provides an interface for getting products again:

Public class concretebuilder implements builder {

Part parta, partb, partc;
Public void buildparta (){
// Here is how to build the parta code
};
Public void buildpartb (){
// Here is how to build the partb code
};
Public void buildpartc (){
// Here is how to build the partb code
};
Public Product getproduct (){
// Return the final assembly result
};
}
 
Complex Object: product:

Public interface product {}

Components of complex objects:

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. getproduct ();"

"The above is the general usage of the builder mode. As for the difference between the builder and abstract factory modes, builder emphasizes the concept that the whole is composed of parts. For example, if you want to obtain the complex object PC, you must first prepare the motherboard, CPU, hard disk, and other simple objects required to construct a PC, and then call the builder's get operation to obtain the final product PC;

The abstract factory mode does not emphasize this relationship, or it can be said that the structure is 'loose coupling '."

"Well, let's look at an image: the builder model is used to make products. Your role is equivalent to the product manufacturer. You know how to use parts to make products, components come from component manufacturers. You can use different types of components or use different manufacturing methods for the same type of components. Abstract Factory is based on the user's perspective, mainly to provide users with product series, users can easily use these product series, but users are not likely to change the product, only existing product series can be used.
For example, the builder mode is suitable for computer enthusiasts and often DIY, while the abstract factory mode is suitable for brands like stable machines, such as IBM and Lenovo ."

"Do you mean that constructor calls the buildpart method of builder in turn, and then calls the getproduct method to obtain the entire product after all the methods are completed? So why don't I add a construct method in builder to call my buildpart method in turn? This reduces the coupling between constructor and builder. In other words, this kind of call is determined at runtime, so it cannot be written in the middle of builder ?"

"As mentioned above, if we put the construction process in builder, we will lose the flexibility of the structure. Take PC as an example. If you construct a PC with a hard drive of 80 GB in builder, but you want to construct another PC with a hard drive of GB; you absolutely do not want to modify your builder any more, so builder is used to construct the part, and the overall structure is obtained by another ctor. Simply put, you can combine these parts by yourself and call the get method to get the product you want ."

"In actual use of Java, we often use the 'pooled 'concept. When the resource provider cannot provide enough resources and these resources need to be shared by many users, you need to use the pool. The 'pooled 'is actually a piece of memory. When the pool has some 'residual' of complex resources (such as the database connection pool, sometimes a connection is interrupted ), if these 'residual 'are recycled, the memory usage efficiency will be improved and the pool performance will be improved. Focus on assembly !"

"Oh, I understand a little ...... You really do !"

"Where to go !!!"

"Where do you think I boast of you ??? I said you haven't installed the machine for a long time. You can do it !!!"

"I'm dizzy ...... Inverted! You Nini !"

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.