• 分步构建【其他模式】


    分步构建模式

    public class StepBuilder {
        /**
         * Step Builder Pattern【分步构建模式】:
         * 明确指定具体的构建步骤执行什么操作
         */
        @Test
        public void all() {
            final Long id = Long.valueOf(1);
            final User user = User.stepBuilder()
                    .id(id)
                    .name("zxd")
                    .hoppy("sleep")
                    .hoppy("eat")
                    .hoppy("work")
                    .build();
            assertEquals(id,user.getId());
        }
    }
    @Getter
    class User{
        private final Long id;
        private final String name;
        private final List<String> hoppies;
    
        @SuppressWarnings("synthetic-access")
        public static IdBuiler stepBuilder() {
            return new UserStepBuilder();
        }
    
        private User(Long id, String name, List<String> hoppies) {
            this.id = id;
            this.name = name;
            this.hoppies = hoppies;
        }
    
        public interface IdBuiler {
            NameBuilder id(Long id);
        }
    
        public interface NameBuilder{
            HoppyBuilder name(String name);
        }
    
        public interface HoppyBuilder{
            HoppyBuilder hoppy(String hoppy);
            User build();
        }
    
        private static class UserStepBuilder implements IdBuiler,NameBuilder,HoppyBuilder{
            private Long id;
            private String name;
            private List<String> hoppies;
    
            @Override
            public User build() {
                return new User(id, name, hoppies);
            }
    
            @Override
            public HoppyBuilder hoppy(String hoppy) {
                hoppies.add(hoppy);
                return this;
            }
    
            @Override
            public HoppyBuilder name(String name) {
                this.name=name;
                hoppies=Lists.newArrayList();
                return this;
            }
    
            @Override
            public NameBuilder id(Long id) {
                this.id=id;
                return this;
            }
        }
    }
    
    
  • 相关阅读:
    JProfiler_SN_8_x key
    java格式化百分比
    获取每月第一天最后一天 java
    java 获取昨天日期
    eclipse git提交代码
    SIT与UAT的分别
    Spring <context:annotation-config/> 说明
    Hibernate日期映射类型
    Oracle查询备注信息
    Log4J入门
  • 原文地址:https://www.cnblogs.com/zhuxudong/p/10217142.html
Copyright © 2020-2023  润新知