美文网首页
设计模式之建造者模式

设计模式之建造者模式

作者: Tinyspot | 来源:发表于2023-07-10 17:05 被阅读0次

    1. 建造者模式(Builder)

    • 定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示
    • 两个方面
      • 构建与表示分离
      • 构建的行为顺序不同
    • 在内部组装完毕,然后再显示出来

    2. 应用

    2.1 例程

    @Test
    public void testBuilder() {
        IBuilder builder = new ConcreteBuilder();
        OrderInfo orderInfo = builder.builder()
                .buildOrder("order")
                .buildOrderItem("orderItem")
                .buildFeature("feature")
                .build();
    }
    
    public interface IBuilder {
        IBuilder builder();
        IBuilder buildOrder(String order);
        IBuilder buildOrderItem(String orderItem);
        IBuilder buildFeature(String feature);
        OrderInfo build();
    }
    
    public class ConcreteBuilder implements IBuilder {
        private OrderInfo orderInfo;
    
        @Override
        public IBuilder builder() {
            orderInfo = new OrderInfo();
            return this;
        }
    
        @Override
        public IBuilder buildOrder(String order) {
            orderInfo.setOrder(order);
            return this;
        }
    
        @Override
        public IBuilder buildFeature(String feature) {
            orderInfo.setFeature(feature);
            return this;
        }
    
        @Override
        public IBuilder buildOrderItem(String orderItem) {
            orderInfo.setOrderItem(orderItem);
            return this;
        }
    
        @Override
        public OrderInfo build() {
            return orderInfo;
        }
    }
    

    2.2 普通类构建

    @Test
    public void testBuilder() {
        // 链式写法
        User user = new UserBuilder().builder()
                .addName("Tinyspot")
                .addAge(20)
                .build();
    }
    
    public class UserBuilder {
        private User user;
    
        public User build() {
            return user;
        }
    
        public UserBuilder builder() {
            user = new User();
            return this;
        }
    
        public UserBuilder addName(String name) {
            user.setName(name);
            return this;
        }
        
        public UserBuilder addAge(Integer age) {
            user.setAge(age);
            return this;
        }
        
        // ...
    }
    

    思考:
    此处return this的应用

    2.3 静态内部类方式

    @Test
    public void testBuilder() {
        User user = new User.Builder().builder()
                .addName("Tinyspot")
                .addAge(20)
                .build();
    }
    
    @Data
    public class User {
        private String name;
        private Integer age;
        private Integer status;
        private Integer salary;
        private Long total;
    
        // 静态内部类方式
        public static class Builder {
            private User user;
    
            public User build() {
                return user;
            }
    
            public Builder builder() {
                user = new User();
                return this;
            }
    
            public Builder addName(String name) {
                user.setName(name);
                return this;
            }
    
            public Builder addAge(Integer age) {
                user.setAge(age);
                return this;
            }
    
            // ...
        }
    }
    

    相关文章

      网友评论

          本文标题:设计模式之建造者模式

          本文链接:https://www.haomeiwen.com/subject/dsneudtx.html