• Java设计模式-创建型模式-建造者模式


    建造者模式

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

    案例

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

    以 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);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116

    测试代码如下:

    @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");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    与工厂模式的区别:

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

    这里引申一下建造者模式简单构件方法——使用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);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    测试代码:

    /**
         * 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");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    【jquery Ajax】接口的学习与Postcode插件的使用
    【Java集合类面试二十九】、说一说HashSet的底层结构
    KubeSphere 在互联网医疗行业的应用实践
    工厂无线wifi短信验证码认证方案
    深度学习笔记之优化算法(六)RMSprop算法的简单认识
    STM8与汇编语言(9)--EEPROM应用
    Meta Llama 3本地部署
    Centos磁盘爆满_openEuler系统磁盘爆满清理方法---Linux工作笔记060
    【新书推荐】人工智能的当下,测试团队如何敏捷转型 —— 无测试组织
    CAPL函数Test Node中,关闭总线,关闭节点,停发报文应该怎么做?
  • 原文地址:https://blog.csdn.net/ren9436/article/details/134340607