Home  >  Article  >  Java  >  What is Java Builder Pattern

What is Java Builder Pattern

WBOY
WBOYforward
2023-05-02 12:04:061282browse

1. Ask a question

Suppose we need to build a house: the process is piling, building walls, and capping. There are various types of houses, such as ordinary houses, high-rise buildings, and villas. Although the process is the same for all kinds of houses, the requirements are not the same. 3) Please write a program to complete the requirements.

The traditional idea should be in the form of the following class diagram. .

What is Java Builder Pattern

#The advantage of this way of writing is that it is easier to understand and easy to operate.

The disadvantages are: the designed program structure is too simple, there is no cache layer object designed, and the program cannot be expanded and maintained well. In other words, this design scheme encapsulates the product (ie: house) and the process of creating the product (ie: house building process), and the coupling is enhanced.

Solution: Decouple the product and product building process => Builder pattern.

2. What is builder pattern?

  • Builder Pattern (Builder Pattern), also called generator pattern, is an object construction pattern. It can abstract the construction process of complex objects (abstract category), making this abstract Different implementation methods of the process can construct objects with different performances (properties).

  • The builder pattern is to create a complex object step by step. It allows the user to only specify the type of the complex object. and content can be built without the user needing to know the specific construction details inside.

And there are four major roles in the builder mode:

  • Product (product role): A specific product object.

  • Builder (abstract builder): Creates an interface/abstract class specified by each component of a Product object.

  • ConcreteBuilder (concrete builder): implements the interface, builds and assembles various components.

  • Director (director): Constructs an object using the Builder interface . It is mainly used to create a complex object. It mainly has two functions, one is: Isolating the production process of the customer and the object, the other is: Responsible for controlling the production process of the product object.

3.Case code

If the above case of building a house is solved using the builder pattern, then the class diagram is as follows. (The four major roles are all in it), and the House class is As for the specific product (the entity of the house we want to build), HouseBuilder is an abstract builder. The specific construction process is not implemented internally, but is completed by several subclasses below it. These subclasses are the concrete builder (CommonHouse , HighBuilding), the commander is HouseDirector, which is responsible for the construction process of product objects (what type of house do I want to build). The final Client is our test class.

What is Java Builder Pattern

package com.szh.builder;
 
public class House {
    private String basic;
    private String wall;
    private String roof;
 
    //setter and getter
}
package com.szh.builder;
 
//抽象的建造者
public abstract class HouseBuilder {
 
    protected House house = new House();
 
    //将建造的流程写好, 抽象的方法
    public abstract void buildBasic();
    public abstract void buildWall();
    public abstract void buildRoof();
 
    //建造房子好, 将产品(房子)返回
    public House buildHouse() {
        return house;
    }
 
}
package com.szh.builder;
 
public class CommonHouse extends HouseBuilder {
    @Override
    public void buildBasic() {
        System.out.println(" 普通房子打地基5m.... ");
    }
 
    @Override
    public void buildWall() {
        System.out.println(" 普通房子砌墙10cm.... ");
    }
 
    @Override
    public void buildRoof() {
        System.out.println(" 普通房子添加屋顶.... ");
    }
}
package com.szh.builder;
 
public class HighHouse extends HouseBuilder {
    @Override
    public void buildBasic() {
        System.out.println(" 高楼打地基100m.... ");
    }
 
    @Override
    public void buildWall() {
        System.out.println(" 高楼砌墙20cm.... ");
    }
 
    @Override
    public void buildRoof() {
        System.out.println(" 高楼添加透明屋顶.... ");
    }
}
package com.szh.builder;
 
//指挥者,这里去指定制作流程,返回产品
public class HouseDirector {
 
    HouseBuilder houseBuilder;
 
    //构造器传入 houseBuilder
    public HouseDirector(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }
 
    //通过setter传入 houseBuilder
    public void setHouseBuilder(HouseBuilder houseBuilder) {
        this.houseBuilder = houseBuilder;
    }
 
    //如何处理建造房子的流程,交给指挥者
    public House constructHouse() {
        houseBuilder.buildBasic();
        houseBuilder.buildWall();
        houseBuilder.buildRoof();
        return houseBuilder.buildHouse();
    }
}
package com.szh.builder;
 
public class MainTest {
    public static void main(String[] args) {
        //盖普通房子
        CommonHouse commonHouse = new CommonHouse();
        //准备创建房子的指挥者
        HouseDirector houseDirector = new HouseDirector(commonHouse);
        //完成盖房子,返回产品(普通房子)
        houseDirector.constructHouse();
 
        System.out.println("--------------------------");
 
        //盖高楼
        HighHouse highHouse = new HighHouse();
        //重置建造者
        houseDirector.setHouseBuilder(highHouse);
        //完成盖房子,返回产品(高楼)
        houseDirector.constructHouse();
    }
}

What is Java Builder Pattern

3.Builder pattern in JDK

Let’s take a look at the StringBuilder class, its parent class, and the related interfaces implemented by the parent class.

What is Java Builder Pattern

What is Java Builder Pattern

What is Java Builder Pattern

  • ##Appendable interface defines multiple append methods (abstract methods) , that is, Appendable is an abstract builder and defines abstract methods.

  • AbstractStringBuilder implements the Appendable interface method. The AbstractStringBuilder here is already a builder, but it cannot be instantiated.

  • StringBuilder acts as a commander and a specific builder. The implementation of the construction method is completed by AbstractStringBuilder, and StringBuilder inherits AbstractStringBuilder.

4. Summary of Builder Pattern

  • The client (the user program) does not need to know the details of the internal composition of the product, and the product itself and the product The creation process is decoupled so that the same creation process can create different product objects.

  • Each specific builder is relatively independent and has nothing to do with other specific builders, so it is easy to replace specific builders or add new specific builders. Users use different The specific builder can get different product objects.

  • allows for more granular control over the product creation process. Breaking down the creation steps of complex products into different methods makes the creation process clearer and makes it easier to use programs to control the creation process.

  • Adding new concrete builders does not require modifying the code of the original class library. The commander class is programmed for the abstract builder class. The system is easy to expand and complies with the "opening and closing principle".

  • The products created by the builder mode generally have more in common and their components are similar. If the differences between the products are large, it is not suitable to use the builder mode, so it is used The scope is subject to certain restrictions.

  • If the internal changes of the product are complex, it may result in the need to define many specific builder classes to implement such changes, causing the system to become very large. Therefore, in this case, it is necessary to Consider whether to choose builder mode.

  • The abstract factory pattern implements the creation of product families. A product family is a series of products: product combinations with different classification dimensions. Using the abstract factory pattern does not require concern about the construction process. Just care about which products are produced by which factories. The builder mode requires building a product according to a specified blueprint. Its main purpose is to produce a new product by assembling spare parts.

The above is the detailed content of What is Java Builder Pattern. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete