建造者模式(或者称为生成器(构建器)模式)

一、什么是建造者模式?

将复杂对象的构建与表示进行分离,使得统一的构建过程,可以创建出不同的对象表现模式

就是将复杂对象里面的成员变量,设置不同的值,使得生成出来的对象拥有不同的属性值;

二、特点

要解决的问题:复杂对象使用不同的构建过程,产生拥有不同对象属性的对象,调用者只需要设定不同的属性值,不需要了解属性值之间是怎么构建出来对象的;

抽象建造者:抽象创建者接口

实际建造者:抽象创建者的不同实现

产品:复杂产品

指挥者:用来与调用者交互,实现通过抽象创建者生产出产品的流程

三、角色和实现

实现方式一:

  • 抽象建造者
java 复制代码
public interface CarBuilderInterface {

    Object builderWheel();


    Object builderFrame();

    Object builderCoreComponent();


}
  • 具体建造者
java 复制代码
public class CarBuilderInterfaceImpl implements CarBuilderInterface {
    @Override
    public Object builderWheel() {
        System.out.println("生产了一个轮子");
        return "返回轮子";
    }

    @Override
    public Object builderFrame() {
        System.out.println("生产了一个车架");
        return "返回车架";
    }

    @Override
    public Object builderCoreComponent() {
        System.out.println("生产了核心组件");
        return "返回核心组件";
    }
}
  • 具体的产品
java 复制代码
public class CarProduct {

    private String wheel;

    private String frame;

    private String coreComponent;

    public String getWheel() {
        return wheel;
    }

    public void setWheel(String wheel) {
        this.wheel = wheel;
    }

    public String getFrame() {
        return frame;
    }

    public void setFrame(String frame) {
        this.frame = frame;
    }

    public String getCoreComponent() {
        return coreComponent;
    }

    public void setCoreComponent(String coreComponent) {
        this.coreComponent = coreComponent;
    }
}
  • 指挥者Director:实例化建造者,通过建造者组织具体的产品
java 复制代码
public class CarBuilderDirector {

    private CarBuilderInterface carBuilderInterface;

    public CarBuilderDirector(CarBuilderInterface carBuilderInterface) {
        this.carBuilderInterface = carBuilderInterface;
    }


    public CarProduct builderCarProduct() {
        CarProduct carProduct = new CarProduct();
        carProduct.setWheel(carBuilderInterface.builderWheel().toString());
        carProduct.setFrame(carBuilderInterface.builderFrame().toString());
        carProduct.setCoreComponent(carBuilderInterface.builderCoreComponent().toString());
        return carProduct;
    }
}
  • 当需要使用建造者模式进行对象构建时:
java 复制代码
public class Client {

    public static void main(String[] args) {

        CarBuilderInterface carBuilderInterface = new CarBuilderInterfaceImpl();

        CarBuilderDirector carBuilderDirector = new CarBuilderDirector(carBuilderInterface);

        CarProduct carProduct = carBuilderDirector.builderCarProduct();
        System.out.println("生成了一个车:" + carProduct.getWheel() + "," + carProduct.getFrame() + "," + carProduct.getCoreComponent());
    }
}

实现方式二:使用静态内部类的方式实现

java 复制代码
/**
 * 通过内部类的形式实现建造者模式,并且可以实现链式调用
 */
public class CarBuilderExample {

    private String wheel;

    private String frame;

    private String coreComponent;
    

    private CarBuilderExample(String wheel, String frame, String coreComponent) {
        this.wheel = wheel;
        this.frame = frame;
        this.coreComponent = coreComponent;
    }

    public static class CarBuilderExampleBuilder {
        private String wheel;

        private String frame;

        private String coreComponent;

        public static CarBuilderExampleBuilder builder() {
            return new CarBuilderExampleBuilder();
        }

        public CarBuilderExampleBuilder wheel(String wheel) {
            this.wheel = wheel;
            return this;
        }

        public CarBuilderExampleBuilder frame(String frame) {
            this.frame = frame;
            return this;
        }

        public CarBuilderExampleBuilder coreComponent(String coreComponent) {
            this.coreComponent = coreComponent;
            return this;
        }

        public CarBuilderExample builderExample() {
            return new CarBuilderExample(wheel, frame, coreComponent);
        }

    }

    public static void main(String[] args) {
        CarBuilderExampleBuilder builder = CarBuilderExampleBuilder.builder();
        CarBuilderExample carBuilderExample = builder
                .wheel("轮子")
                .frame("车架+")
                .coreComponent("核心部件+").builderExample();
        System.out.println(carBuilderExample.coreComponent + carBuilderExample.frame + carBuilderExample.wheel);
    }

}
复制代码
lombok包里面的@Builder也是通过这种模式实现的

四、与工厂方法和抽象工厂之间的区别

工厂是指创建一个类型的多种实现

抽象工厂是指创建一系列类型的多种实现

相关推荐
TT-Kun1 天前
设计模式 之 建造者模式(C++)
c++·设计模式·建造者模式
依恋、阳光4 天前
建造者模式构建对象
java·设计模式·建造者模式·类与对象
wy02_7 天前
【设计模式】 建造者模式和原型模式
设计模式·建造者模式·原型模式
ox00808 天前
C++ 设计模式-建造者模式
c++·设计模式·建造者模式
小王子102423 天前
设计模式Python版 建造者模式
python·设计模式·建造者模式
找了一圈尾巴1 个月前
设计模式-建造者模式、原型模式
设计模式·建造者模式
等一场春雨1 个月前
Java设计模式 五 建造者模式 (Builder Pattern)
java·设计模式·建造者模式
3D小将1 个月前
3D 模型格式转换之 STP 转 STL 深度解析
3d·建造者模式
Leaf吧1 个月前
java 设计模式 建造者模式
java·设计模式·建造者模式