java 设计模式--创建者模式

参考:Java常见设计模式总结

概念

概念理解一:将复杂对象的创建过程分解在不同的方法中,不同的创建过程组装成不同对象。对象的创建与产品本身分离开,使得对象的创建过程更加清晰。例如:旅游套餐售卖场景。
一个套餐大概会由机票、车票、酒店、景点门票组成。
套餐A: 机票1 + 酒店1 + 景点门票1
套餐B: 车票1 + 景点门票2
套餐C: 机票1 + 酒店1
等等
概念理解二:如果对象的构造的入参十分复杂时,构造对象需要转为链式写法时
第三方库举例:
1、由于产品的构造函数参数太多或者组合方式太多
Mybaitis 和 SqlSessionFactoryBuilder
2、将 set 方法转成链式写法
StringBuilder,StringBuffer.append()方法

java 设计模式--创建者模式_第1张图片

抽象建造者 Builder:相当于建筑蓝图,声明了创建 Product 对象的各个部件指定的抽象接口。
具体建造者 ConcreteBuilder:实现Builder抽象接口,构建和装配各个部件,定义并明确它所创建的表示,并提供一个检索产品的接口。
指挥者 Director:构建一个使用 Builder 接口的对象。主要有两个作用,一是隔离用户与对象的生产过程,二是负责控制产品对象的生产过程。
产品角色 Product:被构造的复杂对象。ConcreteBuilder 创建该产品的内部表示并定义它的装配过程,包含定义组成部件的类,包括将这些部件装配成最终产品的接口。

抽象构造者

@Data
public abstract class Builder {
    Package pack = new Package();
    public abstract void buildAirTicket();
    public abstract void buildHotel();
    public abstract void buildTripTicket();
    public abstract void buildCarTicket();

    public Package getPack(){
        return pack;
    }
}

套餐A具体的构造者 PackageAConcreteBuilder

@Data
public class PackageAConcreteBuilder extends Builder {
//    套餐A: 机票1 + 酒店1 + 景点门票1
    @Override
    public void buildAirTicket() {
        AirTicket airTicket1 = new AirTicket(1000, 999, "机票1", "0001", 200);
        pack.setAirTicket(airTicket1);
    }

    @Override
    public void buildHotel() {
        Hotel hotel1 = new Hotel(300, 999, "酒店1", 2);
        pack.setHotel(hotel1);
    }

    @Override
    public void buildTripTicket() {
        TripTicket tripTicket1 = new TripTicket(50, 9999, "景点门票1");
        pack.setTripTicket(tripTicket1);
    }

    @Override
    public void buildCarTicket() {
        return;
    }

    @Override
    public Package getPack() {
        return super.getPack();
    }
}

套餐B具体的构造者 PackageBConcreteBuilder

@Data
public class PackageBConcreteBuilder extends Builder{
//    套餐B: 车票1 + 景点门票2
    @Override
    public void buildAirTicket() {

    }

    @Override
    public void buildHotel() {

    }

    @Override
    public void buildTripTicket() {
        TripTicket tripTicket2 = new TripTicket(30, 9999, "景点门票2");
        pack.setTripTicket(tripTicket2);
    }

    @Override
    public void buildCarTicket() {
        CarTicket carTicket1 = new CarTicket(50, 9999, "车票1","0876");
        pack.setCarTicket(carTicket1);
    }

    @Override
    public Package getPack() {
        return super.getPack();
    }
}

套餐模型对象类 Package 相当于图中的 Product

@Data
public class Package {
    AirTicket airTicket;
    Hotel hotel;
    TripTicket tripTicket;
    CarTicket carTicket;

    public Package() {
    }

    public Package(AirTicket airTicket, Hotel hotel, TripTicket tripTicket, CarTicket carTicket) {
        this.airTicket = airTicket;
        this.hotel = hotel;
        this.tripTicket = tripTicket;
        this.carTicket = carTicket;
    }
}

构造者

@Data
public class SystemDirector {
    private Builder builder;

    public void setBuilder(Builder builder){
        this.builder = builder;
    }
    public Package ConcreteBuilder(){
        builder.buildAirTicket();
        builder.buildHotel();
        builder.buildCarTicket();
        builder.buildTripTicket();

       return builder.getPack();
    }
}

如何使用

public static void main(String[] args){
        SystemDirector systemDirector = new SystemDirector(); // 指挥者
        PackageAConcreteBuilder packageABuilder = new PackageAConcreteBuilder();
        PackageBConcreteBuilder packageBBuilder = new PackageBConcreteBuilder();

        systemDirector.setBuilder(packageABuilder);// 指挥操作套餐A
        Package packageA = systemDirector.ConcreteBuilder();//套餐A的具体构造者创造对象     log.info("packageA,airTicket:{},hotel:{},tripTicket:{}",packageA.airTicket.getName(),
                packageA.hotel.getName(),packageA.tripTicket.getName());

        systemDirector.setBuilder(packageBBuilder);// 指挥操作套餐B
        Package packageB = systemDirector.ConcreteBuilder();//套餐B的具体构造者创造对象
        log.info("packageB,carTicket:{},tripTicket:{}",packageB.carTicket.getName(), packageB.tripTicket.getName());
    }

你可能感兴趣的:(java,设计模式,开发语言)