设计模式——建造者模式

一、方式一:由指挥者决定创建的顺序

1、创建一个抽象的建造者类,里面定义创建一个房子的具体步骤,最后定义一个构建完成的房子

//抽象的建造者:方法
public abstract class Builder {
    abstract void builderA();//地基
    abstract void builderB();//钢筋工程
    abstract void builderC();//铺电线
    abstract void builderD();//粉刷
    //完工
    abstract Product getProduct();
}

2、创建一个建造者类,此类是继承了抽象建造者类,实现了抽象建造者类的里面的方法步骤(也就是实现了建造房子的具体步骤)需要由指挥者调用这些个建造房子的步骤

//具体的建造者:工人
//此时没有指挥者去指挥工人完成以下的方法,所以需要一个指挥者类
public class Work extends Builder{
    private Product product;
    public Work(){
        //注意这里的产品是由工人创建的
        product = new Product();
    }
    @Override
    void builderA() {
        product.setBuildA("地基");
        System.out.println("建造了地基");
    }
    @Override
    void builderB() {
        product.setBuildB("钢筋水泥");
        System.out.println("建造了钢筋水泥");
    }
    @Override
    void builderC() {
        product.setBuildC("铺电线");
        System.out.println("铺设了电线");
    }
    @Override
    void builderD() {
        product.setBuildD("粉刷");
        System.out.println("粉刷了墙面");
    }
    @Override
    Product getProduct() {
        return product;
    }
}

3、创建一个房子,里面有很多组件,也就是,完成了这些组件才能说是房子建造完成

//具体的产品类,产品要被建成,必须给它的内部属性进行设置,通过set方法设置
//一般是被工人进行设置
public class Product {
    private String buildA;
    private String buildB;
    private String buildC;
    private String buildD;
    public String getBuildA() {
        return buildA;
    }
    public void setBuildA(String buildA) {
        this.buildA = buildA;
    }
    public String getBuildB() {
        return buildB;
    }
    public void setBuildB(String buildB) {
        this.buildB = buildB;
    }
    public String getBuildC() {
        return buildC;
    }
    public void setBuildC(String buildC) {
        this.buildC = buildC;
    }
    public String getBuildD() {
        return buildD;
    }
    public void setBuildD(String buildD) {
        this.buildD = buildD;
    }
    @Override
    public String toString() {
        return "Product{" +
                "buildA='" + buildA + '\'' +
                ", buildB='" + buildB + '\'' +
                ", buildC='" + buildC + '\'' +
                ", buildD='" + buildD + '\'' +
                '}';
    }
}

4、创建一个指挥者类,去指挥建造房子的步骤,最后得到房子

//指挥:核心,负责构建一个工程,如何构建,由指挥者决定
public class Director {
    public Product build(Builder builder){
        //这是指挥者指挥工人去完成建造,以下的四个步骤可以不同
        builder.builderA();
        builder.builderB();
        builder.builderC();
        builder.builderD();
        return builder.getProduct();
    }
}

二、与以上不同的是,可以通过链式编程,去任意组合建造顺序,假如用在麦当劳,可以自己去自定义套餐,也有默认套餐

1、创建一个套餐类,里面的套餐是被默认赋值的,然后的话,也可以通过更改默认套餐做到任意搭配的目的,里面有set方法,用于方便赋值给套餐里面的属性

public class Product {

    private String builderA = "薯条";
    private String builderB = "汉堡";
    private String builderC = "可乐";
    private String builderD = "炸鸡";

    public void setBuilderA(String builderA) {
        this.builderA = builderA;
    }

    public void setBuilderB(String builderB) {
        this.builderB = builderB;
    }

    public void setBuilderC(String builderC) {
        this.builderC = builderC;
    }

    public void setBuilderD(String builderD) {
        this.builderD = builderD;
    }
}

2、创建一个抽象的建造者类,里面的方法是选择套餐的方法,返回值定义为此类,方便链式编程,最后返回套餐类,不需要指挥者,用户相当于指挥者,可以任意搭配套餐

public interface Builder {

    Builder builderA(String msg);
    Builder builderB(String msg);
    Builder builderC(String msg);
    Builder builderD(String msg);

    //完工
    Product getProduct();


}

3、创建一个抽象建造者类的实现类,假设没有修改套餐,则用原来的套餐,如果修改套餐,则会任意组合,最后返回套餐

public class Work implements Builder{
    private Product product;
    public Work(){
        product = new Product();
    }

    @Override
    public Builder builderA(String msg) {
        product.setBuilderA(msg);
        return this;
    }

    @Override
    public Builder builderB(String msg) {
        product.setBuilderB(msg);
        return this;
    }

    @Override
    public Builder builderC(String msg) {
        product.setBuilderC(msg);
        return this;
    }

    @Override
    public Builder builderD(String msg) {
        product.setBuilderD(msg);
        return this;
    }

    @Override
    public Product getProduct() {
        return product;
    }

    @Override
    public String toString() {
        return "Work{" +
                "product=" + product +
                '}';
    }
}

posted @ 2021-12-23 16:11  刘小呆  阅读(34)  评论(0编辑  收藏  举报