Application of builder model in deep parsing Java design pattern programming _java

Source: Internet
Author: User

Definition: Separates the construction of a complex object from its representation, allowing the same build process to create different representations.
Type: Creating class Mode
Class Diagram:

Four elements

    • Product class: Is generally a more complex object, that is, the process of creating objects is more complex, generally there will be more code volume. In this class diagram, the product class is a concrete class, not an abstract class. In practical programming, a product class can consist of an abstract class and its different implementations, or it can be composed of multiple abstract classes and their implementations.
    • Abstract Builder: The purpose of introducing an abstract builder is to make the concrete process of construction into its subclasses. This makes it easier to expand. There are generally at least two abstract methods, one for building products and one for returning products.
    • Builder: Implement all the methods of an abstract class that are not implemented, typically two tasks: building a product, and returning a product.
    • Director class: Responsible for calling the appropriate builders to build products, the Director class does not typically have a dependency on the product class, and direct interaction with the Director class is the Builder class. In general, the Director class is used to encapsulate variable parts of the program.

Code implementation

Abstract class for the product:

Package Builder;
 
Import java.util.ArrayList;
 
Public abstract class Abstractproduct {
   
  //record execution order
  private arraylist<string> part = new arraylist<string > ();
   
  public abstract void dosomething ();
   
  public abstract void dootherthing ();
   
  Final, subclasses cannot override
  final public void Executive () {for
    (int i=0;i<this.part.size (); i++) {
      String ActionName = This.part.get (i);
      if (Actionname.equals ("dosomething")) {
        this.dosomething ();
      }
      else if (actionname.equals ("dootherthing")) {
        this.dootherthing ();
      }
   
  }} Final, subclasses cannot override
  final public void Setpart (arraylist<string> part) {
    This.part = part;
  }
}

The implementation of the Product class:

Package Builder;
 
The public class Product extends abstractproduct{
 
    //Subclass describes steps public
  void DoSomething () { 
    System.out.println (" Would do something ... ");
  }
   
  The public void dootherthing () {
    System.out.println () is something has done. Otherthing'll be done ... ");
  }

Builder Abstract Class:

Package Builder;
 
Import java.util.ArrayList;
 
Public abstract class Builder {
   
  //set different parts of the product to obtain a different product public 
  abstract void Setpart (arraylist<string> part);
   
  Construction Products Public 
  abstract product buildproduct (); 
}

Builder Implementation class:

Package Builder;
 
Import java.util.ArrayList;
 
public class Concreteproduct extends builder{
   
  Private Product Product = new product ();
   
  public void Setpart (arraylist<string> part) {
    This.product.setPart (part);
  }
   
  Public Product buildproduct () {return
    this.product
  }}
 
 

Director class:

Package Builder;
 
Import java.util.ArrayList;
 
The production order of different products is packaged again, to the high-level shielding details public
class Director {
  private arraylist<string> parts = new arraylist< String> ();
  Private Builder Builder = new Concreteproduct ();
   
  Public Product getaproduct () {
    this.part.clear ();
    This.part.add ("dosomething");
    This.part.add ("dootherthing");
    Builder.setpart (part);
    return builder.buildproduct ();
  }

Client:

 package Builder;
    public class Client {public static void main (string[] args) {Director Director = new Director ();
    SYSTEM.OUT.PRINTLN ("will produce 10 products");
      for (int i=1;i<11;i++) {System.out.println ("first" + i + "product");
      Director.getaproduct (). Executive ();
    System.out.println (); }
  }
}


The advantages of the builder's schema
        First, the builder's schema is packaged well. The use of Builder mode can effectively encapsulate changes in the use of builder mode scenarios, the general product class and builder class is relatively stable, so the main business logic encapsulated in the Director class for the overall can achieve better stability.
        Second, the builder model is easy to extend. If you have new requirements, you can do it by implementing a new builder class, which basically doesn't have to change the code that you've already tested before, so it doesn't introduce risk to the original functionality.
 
difference between builder and Factory mode
       We can see that the builder model is very similar to the factory model, In general, the builder model is only a "director class" role in the factory model. In the class diagram of the builder pattern, if you think of the Director class as the client of the final call, then the rest of the diagram can be considered as a simple factory model.
       The builder pattern is typically used to create more complex objects than the factory pattern, because the object creation process is more complex, so the creation process of the object is made up of a new class--the director class. In other words, the factory pattern is to encapsulate the entire creation process of the object in the factory class, providing the final product to the client by the factory class, whereas in the builder's model, the builder class typically only provides the construction of each component in the product class, and the concrete construction process is delivered to the Director class. The Director class is responsible for organizing the components into a product according to specific rules, and then delivering the assembled products to the client.
 
summary
       Builder mode is similar to Factory mode, they are builder mode and the applicable scenarios are similar. In general, if the product is very complex to build, then use the factory model; If the product is more complex to build, then use the builder model.

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.