Java Description Design Patterns (06): Builder Patterns

Keywords: Programming Mobile github JDK Spring

This article source code: GitHub. Click here || GitEE. Click here

I. Life Scene

Based on the builder model, the software development process is described.

1. Code Implementation

/**
 * Software development based on builder pattern description
 */
public class C01_InScene {
    public static void main(String[] args) {
        BuilderSoft builderSoft = new SoftImpl () ;
        ProjectManager manager = new ProjectManager(builderSoft) ;
        manager.createSoft();
    }
}

/**
 * Development of Mobile Phone Software Products
 */
class MobileSoft {
    // Design
    private String design ;
    // Framework
    private String frame ;
    // Development
    private String develop ;
    // Go online
    private String online ;
    // Omitting GET SET method
}
/**
 * Mobile Phone Software Process Abstraction Class
 */
abstract class BuilderSoft {
    // Preliminary work
    public abstract void earlyWork () ;
    // Medium-term work
    public abstract void midWork () ;
    // Later work
    public abstract void lateWork () ;
    // Mobile Phone Software Completion
    public abstract MobileSoft builderSoft () ;
}

/**
 * Implementation of Mobile Phone Software
 */
class SoftImpl extends BuilderSoft {
    private MobileSoft mobileSoft = new MobileSoft();
    @Override
    public void earlyWork() {
        System.out.println("Pre-design of software...");
    }
    @Override
    public void midWork() {
        System.out.println("Mid-term Software Architecture...");
        System.out.println("Mid-term Software Development...");
    }
    @Override
    public void lateWork() {
        System.out.println("Software Late Update...");
    }
    @Override
    public MobileSoft builderSoft() {
        return mobileSoft ;
    }
}
/**
 * Project Manager: Controlling Project Progress
 */
class ProjectManager {
    private BuilderSoft builderSoft ;
    ProjectManager (BuilderSoft builderSoft){
        this.builderSoft = builderSoft ;
    }
    // Overall project cycle
    public void createSoft (){
        builderSoft.earlyWork();
        builderSoft.midWork();
        builderSoft.lateWork();
        // MobileSoft mobileSoft = builderSoft.builderSoft() ;
        // return mobileSoft ;
    }
}

2. Code Structure Diagram

II. Builder Model

1. Basic concepts

Construction mode is the object's creation mode. Construction mode can separate the description of internal attributes of a product from the production process of the product, so that a construction process can generate product objects with different internal representations. That is to say, a mediation object is used to encapsulate a series of object interactions, so that each object does not need to display calls to each other, and can independently change the interaction between objects.

2. Core role

1) The role of abstract Builder

An abstract interface is given to standardize the construction of each component of product object. It is the specific builder role that directly creates the product object in the pattern.

2) ConcreteBuilder role

The tasks completed include: 1. Implementing the interface declared by the abstract builder Builder and giving the operation of creating product instances. 2. Provide examples of products after completion of construction.

3) Director role

Classes that play this role call specific builder roles to create product objects.

4) Product role

The product is the object of construction. Generally speaking, there are many product classes in a system, and these product classes do not necessarily have a common interface, but can be totally unrelated.

3. Pattern Diagram

4. Source Code Implementation

/**
 * Builder pattern code
 */
public class C02_Builder {
    public static void main(String[] args) {
        Builder builder = new ConcreteBuilder();
        Director director = new Director(builder);
        director.construct();
        Product product = builder.builderProduct();
        System.out.println(product.getAct1());
        System.out.println(product.getAct2());
    }
}
/**
 * Product Category
 */
class Product {
    /*Define the operation of some products*/
    private String act1;
    private String act2;
    public String getAct1() {
        return act1;
    }
    public void setAct1(String act1) {
        this.act1 = act1;
    }
    public String getAct2() {
        return act2;
    }
    public void setAct2(String act2) {
        this.act2 = act2;
    }
}
/**
 * Builder Abstract builder class
 */
abstract class Builder{
    public abstract void builderact1();
    public abstract void builderact2();
    public abstract Product builderProduct();
}
/**
 * Specific builders
 */
class ConcreteBuilder extends Builder{
    private Product product = new Product();
    @Override
    public void builderact1() {
        product.setAct1("Operation 1: Execution...");
    }
    @Override
    public void builderact2() {
        product.setAct2("Operation 2: Execution...");
    }
    @Override
    public Product builderProduct() {
        return product;
    }
}
/**
 *  Director Director category
 */
class Director{
    /*Use builder abstract classes*/
    private Builder builder;
    Director(Builder builder){
        this.builder = builder;
    }
    /*Responsible for calling each construction method*/
    public void construct(){
        builder.builderact1();
        builder.builderact2();
    }
}

III. Practical application

1. Application of JDK

Analysis

Appendable defines multiple append() Abstract methods, Abstract builders.

2) AbstractStringBuilder implements the Appendable interface, which is already the builder, but abstract classes cannot be instantiated.

3) StringBuilder inherits AbstractStringBuilder as both a concrete builder and a commander.

2. Application of Spring

Source Code Implementation

// SERIES OF METHODS
public BeanDefinitionBuilder setParentName(String parentName);
public BeanDefinitionBuilder setFactoryMethod(String factoryMethod);
public BeanDefinitionBuilder setFactoryMethodOnBean(String factoryMethod, String factoryBean);
public BeanDefinitionBuilder addConstructorArgValue(@Nullable Object value);
public BeanDefinitionBuilder addConstructorArgReference(String beanName);
public BeanDefinitionBuilder addPropertyValue(String name, @Nullable Object value);
// Building Bean
public AbstractBeanDefinition getRawBeanDefinition() {
    return this.beanDefinition;
}
public AbstractBeanDefinition getBeanDefinition() {
    this.beanDefinition.validate();
    return this.beanDefinition;
}

The practical application of the above two builder models has a somewhat fragmented feeling.

4. Analysis of Advantages and Disadvantages

  • Advantages: Reduce class interdependence and coupling.
  • Disadvantage: Concreate Builder becomes bulky and complex.

5. Source code address

GitHub·address
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·address
https://gitee.com/cicadasmile/model-arithmetic-parent

Posted by tbuzz on Mon, 02 Sep 2019 07:34:23 -0700