Home  >  Article  >  Java  >  Analyze the case of builder pattern in Java

Analyze the case of builder pattern in Java

王林
王林forward
2023-05-10 10:40:151324browse

What is the Builder Pattern

The definition of the Builder pattern: refers to a design that separates the construction of a complex object from its representation so that the same construction process can create different representations. pattern is called builder pattern. It breaks down a complex object into multiple simple objects and then builds it step by step. It separates change from immutability, that is, the components of the product remain unchanged, but each part can be flexibly selected.

Advantages

1. Good encapsulation, separation of construction and presentation.

2. It has good scalability and each specific builder is independent of each other, which is conducive to the decoupling of the system.

3. The client does not need to know the details of the internal composition of the product. The builder can gradually refine the creation process without any impact on other modules, making it easier to control detailed risks.

Disadvantages

1. The components of the product must be the same, which limits its scope of use.

2. If the internal changes of the product are complex, the builder will also need to make simultaneous modifications, which will cause higher maintenance costs in the future.

Knowledge Points

The builder model and the factory model have different focuses: the builder model focuses on the assembly process of components, while the factory method model focuses more on the creation process of components. But both can be used together.

Builder Pattern Implementation

Case: Miracle Doctor Doudou Alchemy

Product Role (Product): Pill

Abstract Builder (Builder): Abstract Danfang

Concrete Builder: Concrete Danfang

Director: Miracle Doctor Doudou

Miracle Doctor Doudou refines the nine-turn elixir and Tai Void Divine Pill

Note: To save trouble, the pill recipe mentioned here only contains the name and effect of the pill, and does not include materials or the like.

Elixir

The elixir class declares the name and effect of the elixir

public class Dan {
    /* cailiao 丹药的名称
           danhuo 丹药的效果*/
    private String name;
    private String xiaoguo;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getXiaoguo() {
        return xiaoguo;
    }

    public void setXiaoguo(String xiaoguo) {
        this.xiaoguo = xiaoguo;
    }
}

Abstract elixir

Abstract class, new an elixir object , declares that two abstract methods are to name the elixir and give the elixir its effect, and there is also a method to generate the elixir.

//抽象丹方
abstract class DanFang {
    Dan dan = new Dan();

    public abstract void name();

    public abstract void xiaoguo();

    public Dan getDan() {
        return dan;
    }

}

Nine-turn elixir prescription

The specific prescription class inherits the prescription class and implements two abstract methods.

//九转仙丹丹方
public class JiuZhuan extends DanFang {

    @Override
    public void name() {
        dan.setName("九转仙丹");
    }

    @Override
    public void xiaoguo() {
        dan.setXiaoguo("原地成仙");
    }
}

Taixuhuashendandanfang

The specific Danfang class inherits the Danfang class and implements two abstract methods.

//太虚化神丹丹方
public class Taixu extends DanFang {
    @Override
    public void name() {
        dan.setName("太虚化神丹");
    }

    @Override
    public void xiaoguo() {
        dan.setXiaoguo("意念化神");
    }
}

神药豆豆

Declare an elixir attribute, a constructor method with parameters, and an alchemy method.

public class DouDou {
    private final DanFang danLu;

    public DouDou(DanFang danLu) {
        this.danLu = danLu;
    }

    //炼丹方法
    public Dan lian() {
        //放入材料
        danLu.name();
        //释放丹火
        danLu.xiaoguo();
        //成丹
        return danLu.getDan();
    }
}

Test

Refining two pills.

public class Demo {
    public static void main(String[] args) {
        //炼制九转仙丹
        //new一个九转仙丹的丹方
        DanFang jiu = new JiuZhuan();
        //把丹方给豆豆
        DouDou dou = new DouDou(jiu);
        //炼丹生成丹药
        Dan dan = dou.lian();
        System.out.printf("丹药名称:%s   丹药效果:%s", dan.getName(), dan.getXiaoguo());


        System.out.println();

        //炼制太虚化神丹
        //new一个九转仙丹的丹方
        DanFang tai = new Taixu();
        //把丹方给豆豆
        DouDou dous = new DouDou(tai);
        //炼丹生成丹药
        Dan dans = dous.lian();
        System.out.printf("丹药名称:%s   丹药效果:%s", dans.getName(), dans.getXiaoguo());
    }
}

Analyze the case of builder pattern in Java

The above is the detailed content of Analyze the case of builder pattern in Java. 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