The design pattern of the vegetable--the builder pattern

Source: Internet
Author: User


background

Do not underestimate the little things on the small, want to go on a meal, it is necessary to obey the rules, step-by-step careful. I'm sure you've eaten a fast food without salt and eaten a fast food with a lot of salt. Since stir-frying is such a complicated process and must obey the rules, why not give him a death rule? If someone does not follow the rules, the corresponding alert warning will be made. This is the origin of the builder pattern, limiting the rule steps, but developing the rule details. For example, before the dish must be put salt, then the provisions must be put salt, specifically put how much oneself on the situation.


1. Intention to use

If you need to separate the construction of a complex object from its representation (build specific) so that the same build process can create different representations of intent, the builder pattern appears, which is known as the generator pattern, and is known from the name to build (build) an object, An object that creates the same overall process, but with different details.



2. Life Example

The complicated cooking process forgot to put the salt



3. Java Example (framework, JDK, JEE)

Not very careful, if any of the students think where to use, you can add



4. Pattern class Diagram

1) Director: The conductor, who defines the process for creating this complex object

2) Builder: Abstract build role, he is the definition of each rule details, that is, to declare the need to build this object necessary rules and methods

3) ConcreteBuilder: The concrete builder, he implements every rule of builder, because the diversification of the method realizes a variety of different objects


5. Advantages of the Model

Construction Mode : Separates the construction of a complex object from its representation so that the same build process can create different representations;

The order of the internal builds of these complex objects is usually stable, and the number of steps is deterministic, and if it is a complex object that is constantly changing, then the design pattern will be an open and closed principle error!

The benefit of the builder pattern is that the construction code is separated from the presentation code, because the builder hides how the product is assembled, so if you need to change the internal representation of a product, just top a concrete builder.

The builder pattern is when the algorithm that creates the complex object should be independent of the part of the object and the mode in which they are assembled.


6. Comparison with similar models

The builder model separates the internal representation of a product from the product, allowing a construction process to produce objects with different internal representations.

The great advantage of using the builder model is that:

1) The interface has been declared, no implementation of the concrete builder can not be compiled, then the steps will not be missing (the empty implementation is also implemented)

2) The creation of different objects is easy to expand, because the construction and presentation are separated, so the different objects can be well extended.

Compared to other design patterns, this pattern actually has the template method pattern a bit similar, all is a fixed process, realizes the different detail;





The design pattern of the vegetable--the builder pattern

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.