Design Pattern Series (iv) Builder model Builder

Source: Internet
Author: User
Tags static class

Design Pattern Series (iii) the factory model is described in the article, the factory method model and the abstract factory pattern are introduced. This paper mainly introduces the design pattern of builderOverviewIn the software system, sometimes faced with the creation of "a complex object", it is usually composed of the child objects of each part by a certain algorithm; Because of the change of requirements, the various parts of this complex object often face drastic changes, but the algorithms that combine them are relatively stable. How to deal with this change. How to provide a "encapsulation mechanism" to isolate the "various parts of complex objects" changes, so as to maintain the system of "stable construction algorithm" does not change with the need to change. This is the builder model to say.
The builder model separates the internal representation of a product from the product generation process, enabling a build process to generate a product object with a different internal representation.
Construction of Object Nature
In some cases, an object will have some important properties that cannot be used as a complete product until they have the proper value. For example, an e-mail message has a sender's address, a recipient's address, a subject, content, an appendix, and so on, which cannot be sent until the minimum recipient address has been assigned.
In some cases, it makes sense that some of the properties of an object must be assigned in a certain order. Another property cannot be assigned until a property is assigned a value. These circumstances allow the construction of nature itself to involve complex business logic.
At this time, this object is equivalent to a product to be built, and the object of these properties is equivalent to the product parts, the process of building a product is the process of assembling parts. Because the process of assembling parts is complex, the combination process of these "parts" is often "externalized" to an object called a builder, which is returned to the client by a product object in which all parts have been built.
The reason for using "builder" instead of "generator" is that it is more appropriate to "build" and "create" or "build" than to produce a product from a part.
IntentThe builder pattern separates the construction of a complex object from its representation, allowing the same build process to create different representations.
structure of the builder pattern
Builder: Specifies the abstract interface for each part of the creation of the Product object.
ConcreteBuilder: Implements the builder interface to construct and assemble the parts of the product, define and articulate the representations it creates, and provide an interface to retrieve the product.
Director: Constructs an object that uses the Builer interface.
Product: Represents a constructed complex object. ConcreteBuilder creates an internal representation of the product and defines its assembly process, including the classes that define the components, and the interfaces that assemble the parts into the final product.
Advantages1. The "Processing technology" of the builder model is exposed, which makes the builder's model more flexible.
2, decoupling the assembly process and the creation of specific components, so that we do not care about how each part is assembled. The above analysis of the bicycle can clearly see this.
DisadvantageThe 1th advantage of the above can also be said to be the disadvantage of the builder model, because the "processing technology" exposure, so that this model is not very "elegant."
Applicability1, need to generate the Product object has a complex internal structure.
2. The attributes of the product objects that need to be generated depend on each other, and the builder pattern can be forced to generate order.
3. Some of the other objects in the system are used during object creation, which are not readily available during the creation of the Product object.
Implementation Essentials1. The builder model is mainly used to "construct a complex object in a step-by-step way", in which "step-by-step" is a stable algorithm, while the parts of complex objects often change.
2, the product does not need abstract class, especially because of the complexity of the creation of the object's algorithm, which results in the use of this pattern or the pattern applied to the product generation process, the final result may be very different, it is unlikely to extract an abstract product class.
3. The interface method for creating a child part in the creator is not an abstract method but an empty method. Without doing anything, the specific creator only needs to overwrite the desired method, but this is not absolute, especially in the case of text conversions, where the default method of entering the output is a reasonable default operation.
4, the abstract Factory mode (Abtract Factory) that we mentioned above solves the demand change of "the series object", builder mode solves the demand change of "object part", the builder pattern is used in combination with the combination mode (composite pattern).
use of builder in JDK and Android source codeJava.lang.stringbuilder#append ()
Java.lang.stringbuffer#append ()
Java.sql.PreparedStatement
Javax.swing.grouplayout.group#addcomponent ()
The Builder model we use most often in the Android code is Alertdialog.builder
Creating Notifaciton is also using the builder methodSimple Example

Package com.designpattern.builder;

/** *
 Creator mode
 * * 
 @author Chao
 */public
class Product {

	Object part1;
	Object Part2;

	Private Product (Object Part1, Object part2) {
		this.part1 = part1;
		This.part2 = Part2;
	}

	public static class Builder {
		Object part1;
		Object Part2;

		public void Buildpart1 (Object part1) {
			this.part1 = part1;
		}

		public void Buildpart2 (Object part2) {
			this.part2 = part2;
		}

		Public Product Buildprocudt () {return
			new product (part1, part2);
		}
	}
}


Welcome to scan two-dimensional code, attention to public number


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.