“建造者模式”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
第2行: 第2行:
  
 
=== 1.不用建造者有什么麻烦? ===
 
=== 1.不用建造者有什么麻烦? ===
假设我们要自己开发一个RabbitMQ消息队列的客户端,有很多需要初始化的参数,你会怎么做?
+
假设我们要自己开发一个RabbitMQ消息队列的客户端,有很多需要初始化的参数,你会怎么做?<syntaxhighlight lang="java">
 +
package io.github.jihch;
 +
 
 +
public class RabbitMQClientSample1 {
 +
 
 +
    private String host = "127.9.9.1";
 +
 
 +
    private int port = 5672;
 +
 
 +
    private int mode;
 +
 
 +
    private String exchange;
 +
 
 +
    private String queue;
 +
 
 +
    private boolean isDurable = true;
 +
 
 +
    int connectionTimeout = 1000;
 +
 
 +
    private RabbitMQClientSample1(String host, int port, int mode, String exchange, String queue, boolean isDurable,
 +
                                  int connectionTimeout) {
 +
        this.host = host;
 +
        this.port = port;
 +
        this.mode = mode;
 +
        this.exchange = exchange;
 +
        this.queue = queue;
 +
        this.isDurable = isDurable;
 +
        this.connectionTimeout = connectionTimeout;
 +
        if (mode == 1) {//工作队列模式不需要设置交换机,但queue必填
 +
            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) { //路由模式必须设置交换机,但不能设置 queue 队列
 +
            if (exchange == null || exchange.trim().equals("")) {
 +
                throw new RuntimeException("路由模式请设置交换机");
 +
            }
 +
            if (queue != null) {
 +
                throw new RuntimeException("路由模式无需设置队列名称");
 +
            }
 +
        }
 +
        //其他各种验证
 +
    }// end constructor
 +
 
 +
 
 +
    public void sendMessage(String msg) {
 +
        System.out.println("正在发送消息:" + msg);
 +
    }
 +
 
 +
    public static void main(String[] args) {
 +
        RabbitMQClientSample1 client = new RabbitMQClientSample1("192.168.31.210", 5672, 2, "sample-exchange", null,
 +
                true, 5000);
 +
        client.sendMessage("Test");
 +
    }
 +
}
 +
</syntaxhighlight>每次使用构造方法创建新的对象都要传入很多参数(想想生产环境如果按数据表抽象,一张表有多少字段?!就算默认值可以为 null,一个个字段对照填写费劲不费劲?!),其中有一些参数在一些情况下使用默认值就可以,并不是必要填写的,还是改用 set 方法灵活赋值吧

2022年8月28日 (日) 21:25的版本

https://www.bilibili.com/video/BV1rV4y1s7JG

1.不用建造者有什么麻烦?

假设我们要自己开发一个RabbitMQ消息队列的客户端,有很多需要初始化的参数,你会怎么做?

package io.github.jihch;

public class RabbitMQClientSample1 {

    private String host = "127.9.9.1";

    private int port = 5672;

    private int mode;

    private String exchange;

    private String queue;

    private boolean isDurable = true;

    int connectionTimeout = 1000;

    private RabbitMQClientSample1(String host, int port, int mode, String exchange, String queue, boolean isDurable,
                                  int connectionTimeout) {
        this.host = host;
        this.port = port;
        this.mode = mode;
        this.exchange = exchange;
        this.queue = queue;
        this.isDurable = isDurable;
        this.connectionTimeout = connectionTimeout;
        if (mode == 1) {//工作队列模式不需要设置交换机,但queue必填
            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) { //路由模式必须设置交换机,但不能设置 queue 队列
            if (exchange == null || exchange.trim().equals("")) {
                throw new RuntimeException("路由模式请设置交换机");
            }
            if (queue != null) {
                throw new RuntimeException("路由模式无需设置队列名称");
            }
        }
        //其他各种验证
    }// end constructor


    public void sendMessage(String msg) {
        System.out.println("正在发送消息:" + msg);
    }

    public static void main(String[] args) {
        RabbitMQClientSample1 client = new RabbitMQClientSample1("192.168.31.210", 5672, 2, "sample-exchange", null,
                true, 5000);
        client.sendMessage("Test");
    }
}

每次使用构造方法创建新的对象都要传入很多参数(想想生产环境如果按数据表抽象,一张表有多少字段?!就算默认值可以为 null,一个个字段对照填写费劲不费劲?!),其中有一些参数在一些情况下使用默认值就可以,并不是必要填写的,还是改用 set 方法灵活赋值吧