Java设计模式-创建者模式-建造者模式-CSDN博客

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

建造者模式

建造者模式

建造者模式是将一个复杂对象的构件与表示分离使得同样的构件过程可以创建不同的表示。
建造者模式将内部构件的创建和组装分割开一般使用链式编程代码整洁优雅

案例

建造者模式比较简单这里就直接上代码了

以 RabbitMQClient 为例实现建造者模式

public class RabbitMQClient {
    
    // 私有构造目标类的构造方法要传入一个Builder对象
    private RabbitMQClient(Builder builder){
        
    }
    
    // builder类位于目标类的内部并且使用static修饰
    public static class Builder{
        // 保证不可变对象的密闭性
        private String host = "127.0.0.1";

        private int port = 5672;

        private int mode;

        private String exchange;

        private String queue;

        private boolean isDurable = true;

        int connectTime = 1000;
        
        public String getHost() {
            return host;
        }

        public Builder setHost(String host) {
            this.host = host;
            return this;
        }

        public int getPort() {
            return port;
        }

        public Builder setPort(int port) {
            this.port = port;
            return this;
        }

        public int getMode() {
            return mode;
        }

        public Builder setMode(int mode) {
            this.mode = mode;
            return this;
        }

        public String getExchange() {
            return exchange;
        }

        public Builder setExchange(String exchange) {
            this.exchange = exchange;
            return this;
        }

        public String getQueue() {
            return queue;
        }

        public Builder setQueue(String queue) {
            this.queue = queue;
            return this;
        }

        public boolean isDurable() {
            return isDurable;
        }

        public Builder setDurable(boolean durable) {
            isDurable = durable;
            return this;
        }

        public int getConnectTime() {
            return connectTime;
        }

        public Builder setConnectTime(int connectTime) {
            this.connectTime = connectTime;
            return this;
        }

        //    builder提供 build()方法实现目标对象的创建
        public RabbitMQClient build(){
            if (mode == 1) {// 工作队列模式不需设计交换机但是队列名称一定要有
                if (exchange != null) {
                    throw new RuntimeException("工作队列模式不需设计交换机");
                }
                if (queue == null || queue.trim().equals("")) {
                    throw new RuntimeException("工作队列不能为空");
                }
                if (isDurable == false) {
                    throw new RuntimeException("工作队列模式必须开启持久化");
                }
            } else if (mode == 2) {// 路由模式必须设计交换机但是不能设计队列
                if (exchange == null) {
                    throw new RuntimeException("路由模式必须设计交换机");
                }
                if (queue != null) {
                    throw new RuntimeException("路由模式无需设计队列名称");
                }
            }
            return new RabbitMQClient(this);
        }
    }

    public void sendMsg(String msg){
        System.out.println("发送消息"  + msg);
    }

}

测试代码如下

@Test
public void builderTest(){
        RabbitMQClient client = new RabbitMQClient.Builder()
                .setHost("192.168.11.111")
                .setMode(1)
                .setPort(5672)
                .setQueue("queue-test")
                .build();

        client.sendMsg("this is test");
}

与工厂模式的区别

工厂模式注重整体对象的创建建造者模式注重构件的创建然后再将构件组装成一个完整的对象。
一般情况下建造者模式创建的对象更复杂
工厂模式是生产工厂建造者模式是组装工厂

这里引申一下建造者模式简单构件方法——使用lombok 包下面的 @Builder 注解

@Builder 注解

使用@Builder 注解 可以方便快捷使用 建造者模式
下面使使用案例

@Builder
public class RabbitMQClient2 {
    
    private String host = "127.0.0.1";

    private int port = 5672;

    private int mode;

    private String exchange;

    private String queue;

    private boolean isDurable = true;

    int connectTime = 1000;

    public void sendMsg(String msg){
        System.out.println("发送消息"  + msg);
    }

}

测试代码

/**
     * lombok @Builder 注解使用
     */
@Test
public void builder2Test(){
    RabbitMQClient2 client = RabbitMQClient2.builder()
            .host("192.168.11.111")
            .mode(1)
            .port(5672)
            .queue("queue-test")
            .build();

    client.sendMsg("this is test");
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Java

“Java设计模式-创建者模式-建造者模式-CSDN博客” 的相关文章