Java Builder Design Pattern detailed _java

Source: Internet
Author: User

Builder mode (Builder): Separates the build of a complex object from its representation, allowing the same build process to create different representations.
Usage scenarios:

    • When creating complex objects, the algorithms should be independent of the components of the object and the way they are assembled.
    • When the construction process must allow the constructed object to have a different representation.

General class Diagram:

For example: We live in a lot of equipment in the form of assembly, such as desktop computers, then some manufacturers will be introduced with a default configuration of the assembly computer host (here can be used template method mode to achieve), customers can purchase the default configuration of products, You can also ask the manufacturer to reassemble a host with different configurations of different assembly modes. At this point, we can use the builder model to meet the requirements of special customers.
Note that in this example the vendor is reassembling a host, that is, the focus is on each component of the host, which conforms to the usage scenario given in the builder mode above.
The simple code implementation is as follows:

Abstract product classes, using the template method pattern, different products have different "component part" abstract class abstractproduct{protected abstract void part01 (); 
 protected abstract void part02 (); 
 
 protected abstract void part03 (); 
 The template method gives the default assembly way to generate the default product public final Abstractproduct defaultproduct () {part01 (); 
 Part02 (); 
 part03 (); 
 Return this;//Returns the current object, which is the default assembly method of products}//Specific products A, B, different products have implemented different "component part" class Concreteproducta extends abstractproduct{ 
 protected void part01 () {System.out.println ("Product a:part01 () ..."); 
 } protected void part02 () {System.out.println ("Product a:part02 () ..."); 
 } protected void part03 () {System.out.println ("Product a:part03 () ..."); } class CONCRETEPRODUCTB extends abstractproduct{protected void part01 () {System.out.println ("Product b:part01 (). 
 ."); 
 } 
 protected void part02 () {System.out.println ("Product b:part02 () ..."); 
 } protected void part03 () {System.out.println ("Product b:part03 () ..."); }//abstract builder, develop a combination of methods that each product should implement Buildpart () and standard abstract class Ab for producing buildproduct ()stractbuilder{public abstract void Buildpart (); 
Public abstract abstractproduct buildproduct (); } * * Specific builder, if dissatisfied with the default product (i.e., when invoking the Defaultproduct () method in the abstract product), * You can not call it to get the product, but instead use the specific builder to change the production assembly method of the product to get a different product/class Con 
 
 Cretebuildera extends abstractbuilder{private abstractproduct producta = new Concreteproducta (); 
 public void Buildpart () {this.productA.part03 (); 
 This.productA.part02 (); 
 THIS.PRODUCTA.PART01 (); 
 Public Abstractproduct buildproduct () {return this.producta; 
 
 } class Concretebuilderb extends abstractbuilder{private abstractproduct PRODUCTB = new CONCRETEPRODUCTB (); 
 public void Buildpart () {this.productB.part02 (); 
 THIS.PRODUCTB.PART01 (); 
 Specifically omitted a component of product B, such as the function of the part of the customer does not need//this.productB.part03 (); 
 Public Abstractproduct buildproduct () {return THIS.PRODUCTB; }//Director class, prior to holding the builder of each product, providing different ways of assembling for users who need a different default product class director{private Abstractbuilder Buildera = new ConcreteBuilder 
 A (); Private Abstractbuilder BuildeRB = new Concretebuilderb (); 
 Public Abstractproduct getproducta () {This.builderA.buildPart (); 
 return this.builderA.buildProduct (); 
 Public Abstractproduct GETPRODUCTB () {This.builderB.buildPart (); 
 return this.builderB.buildProduct (); 
 }//test class public class Client {public static void main (string[] args) {System.out.println ("Get default product A by template method mode"); 
  
 Abstractproduct defualtproducta = new Concreteproducta (). Defaultproduct (); 
 SYSTEM.OUT.PRINTLN ("Product a" using Director class to obtain different assembly methods); 
 Director Director = new Director (); 
  
 Director.getproducta (); 
 System.out.println ("B" products of different assembly methods using Director Class); 
 DIRECTOR.GETPRODUCTB ();
 } 
}

Test results:
Use template method mode to get default product a
Product a:part01 () ...
Product a:part02 () ...
Product a:part03 () ...
Using the Director class to obtain different assembly methods of product A
Product a:part03 () ...
Product a:part02 () ...
Product a:part01 () ...
Use Director class to obtain different assembly methods of product B
Product b:part02 () ...
Product b:part01 () ...

In fact, in this example, the product class used the template method pattern described in the previous article, that is, Defaultproduct () provides a way to assemble the default component of a product.

But here I have a question that the so-called default assembly mode provided by the template method pattern in the Abstractproduct class is simply to print out a few test words and not actually return a specific product, but in the example above it returns a current object. Do not know how to handle the reasonable?

In addition, after writing a few articles about implementing design Patterns in Java code, it is found that this builder builder pattern seems to combine abstract factory patterns with template method patterns. The above paragraph has said my doubts, as for the abstract factory model, I personally think the above code example of the Director class is similar to the abstract factory specific factory class, but the Director class is responsible for building the product assembly method to return a product, perhaps this is the " Build "It appears that the builder model focuses on the assembly of parts of the product, while the abstract factory model focuses only on the production of a final product.

Before I read a word is probably said: computer problems if difficult to solve, can be increased by an intermediate layer to deal with. Now think about it, as if the abstract factory and builder patterns all use this "principle" to achieve the desired effect. For example, there is an abstract factory class in abstract factory, and in builder there is a Director class, which in the end encapsulates some of the details and decoupling it from the implementation and use of the two.

I think that we must first understand the various patterns of attention and the application of the scene before you can better grasp these.

Maybe these patterns are all created patterns and I don't have any actual combat experience that makes me a little confused about them ... Not afraid, in their full realization of the process of a little thought, slowly applied to the actual should be gradually understood.

The above is the entire content of this article, I hope that the learning of everyone inspired.

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.