Java design Pattern-constructor mode (Builder)

Source: Internet
Author: User

Construction mode is the creation mode of the object. The construction mode divides the internal objects of a product from the production process of the product, allowing a construction process to produce objects with different internal representations.
Construction of Object properties:
In some cases, an object can have some important properties, and the object cannot be used as a complete product until they have the proper value. For example, an e-mail message has a sender address, a recipient address, a subject, a content, an appendix, and so on, and the e-mail message 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 uses a Director object and a concrete builder object to build all the parts, creating a complete product object. The builder model hides the structure of the product and the construction of the parts of the product, separating the responsibility for directing the construction process from the responsibility of the concrete builder's parts, and achieving the purpose of dividing and encapsulating the responsibilities.
Structure of the constructor pattern:
  
abstract Builder (builder) role:An abstract interface is given to standardize the construction of each component of the Product object. Typically, this interface is independent of the application's business logic. The direct creation of the Product object in the schema is the Concrete Builder (concretebuilder) role. The concrete builder class must implement two methods required by this interface: one is the construction method (BuildPart1 and BuildPart2) and the other is the return structure method (Retrieveresult). In general, the number of parts contained in the product corresponds to the number of construction methods. In other words, how many parts there are, how many corresponding construction methods.
Concrete Builder (concretebuilder) role:As part of this role, there are classes that are closely related to applications that create instances of the product under application invocation. The tasks to be accomplished by this role include: 1. Implement the interface declared by the abstract Builder builder, and give a step-by-step procedure for creating a product instance. 2. After the completion of the construction process, provide an example of the product.
Director role:The class that holds the role invokes the concrete builder role to create the Product object. It should be noted that the Director's role does not have the specific knowledge of the product class, the real knowledge of the product class is the specific builder role.
Product Role:Products are complex objects in construction. In general, there will be more than one product class in a system, and these product classes do not necessarily have a common interface and can be completely unrelated.
The Director role is the role of dealing with clients. The director divides the client's request to create a product into a build request for each part, and then delegates the request to the specific builder role. The concrete builder's role is to do concrete construction work, but it is not known to the client.
In general, there is a corresponding concrete builder class for each product class. These products should have the same number of parts, and each one has a construction method corresponding to all the builders ' roles.
The code is as follows:

 Public  class concretebuilder implements Builder{    PrivateProduct Product =NewProduct ();/** * Product Part construction Method 1 */     Public void BuilderPart1() {System.out.println ("Building the first part"); Product.setpart1 ("Id:123"); }/** * Product Part construction Method 2 */     Public void BuilderPart2() {System.out.println ("Building a second part"); Product.setpart2 ("Name: Transformers"); }/** * Product Return method * /     PublicProductRetrieveresult() {returnProduct }}
public   class  director  { /** * holds the currently used constructor object */ private  B    Uilder Builder; public  director  (Builder builder)        {super  ();    this . Builder = Builder; } public  void          Construct  () {builder.builderpart1 ();    Builder.builderpart2 (); }}
publicstaticvoidmain(String[] args) {        new ConcreteBuilder();        new Director(builder);        director.construct();        Product product = builder.retrieveResult();        System.out.println(product.getPart1()+" "+product.getPart2());    }

Java design Pattern-constructor mode (Builder)

Related Article

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.