Build Mode (builder)

Source: Internet
Author: User

Construction mode is the creation mode of the object. The construction mode separates the internal representation of a product from the production process, allowing a construction process to produce a product object with different internal representations.

Internal representation of the product

A product often has different components as part of the product. For example, an e-mail message has a sender address, a recipient address, a subject, a content, an appendix, and so on, and the email cannot be sent until the minimum recipient address is assigned. In some cases, some properties of an object must be assigned in a certain order to make sense. Another property cannot be assigned to a property until it is assigned a value. These conditions make the construction of nature itself related to 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 parts of the product, the process of building the product is the process of building parts. Because the process of building the parts is complex, the construction of these parts is often "externally" to another object called the builder, and the object returned to the client is a product object where all the parts have been built.

The construction model is well suited to this situation. The construction mode hides the product structure and the part construction process of the product to the client, and achieves the purpose of dividing and encapsulating the responsibility.

A schematic implementation of the system is given below. The system involves four roles:

    • Abstract Builder role: This is an abstract interface to standardize the construction of various components of a Product object. The interface generally consists of two methods, one of which is the construction method: BuildPart1 (), BuildPart2 () in this example. The other is the result return method: The Retrieveresult () method in this case. In general, the number of parts contained in the product is equal to the number of construction methods. In other words, how many parts there are in the construction method.
    • Concrete Builder Role (ConcreteBuilder): The concrete implementation of the abstract builder role, which is closely related to the application, which creates an instance of the product under the call of the application. The main tasks that this role accomplishes are:
    1. Implement the interface declared by the abstract builder role, and give a step-by-step procedure for product creation
    2. When the construction process is complete, provide an example of the product
    • Director role (director): The class that holds the role invokes the concrete builder role to complete the creation of the Product object.
    • Product role: A product is a complex object in construction. In general, there will be more than one product class in a system, and these product classes may not necessarily have a common interface, and they can be completely irrelevant.

The Director role is the role of dealing with clients. The director role divides the requests for client-created products into construction requests for individual parts, and then delegates these requests to the specific builder role. The concrete builder's role is to do concrete construction work, but it is not known to the client.

Here is the system source code:

/* * Director role * */public class Director {private Builder builder;/* * Product Build method, responsible for invoking the build method of individual parts * */public void construct () {Build Er=new ConcreteBuilder (); Builder.buildpart1 (); Builder.buildpart2 (); Builder.retrieveresult ();}}

/* * Abstract Builder Role * */public abstract class Builder {/* * Product Part construction method * */public abstract void BuildPart1 ();/* * Product Part Building method * */PUBL IC abstract void BuildPart2 ();/* Product return method * */public abstract product retrieveresult ();}

/* Specific Builder Role * */public class ConcreteBuilder extends Builder {private Product product=new product (); @Overridepublic void BuildPart1 () {//Todo auto-generated method stub} @Overridepublic void BuildPart2 () {//Todo auto-generated method stub} @Ov Erridepublic Product Retrieveresult () {//TODO auto-generated method Stubreturn Product;}}

/* Product Role * */public class product {}

Here is just a schematic implementation of the system, the concrete application of the builder model, please refer to another article

Build Mode (builder)

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.