链式编程介绍

链式编程:将多个操作(多行代码)使用点语法(.)链接成一行代码,增强代码可读性。

条件:方法调用时每次返回方法本身。

参考链接:Java链式编程学习 - 简书 (jianshu.com)

实现静态编程的三种方式:

  1. return this方法

    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("Alice").append("bob").append("tom");
    
    @Override
        public StringBuilder append(Object obj) {
            return append(String.valueOf(obj));
        }
    @Override
        public StringBuilder append(String str) {
            super.append(str);
            return this;
        }
    
  2. 静态初始化方法of

    构造函数全部私有,使用静态方法进行初始化。

    静态方法为类所有,可以通过对象来使用,也可以通过类来使用。

    	private Pageable(long page, int size) {
            this.page = page;
            this.size = size;
        }
    
        private Pageable(long page, int size, long count) {
            this(page, size);
            this.count = count;
        }
    
        public static Pageable of(long page, int size) {
            return of(page, size, 0);
        }
    
        public static Pageable of(long page, int size, long count) {
            return new Pageable(page, size, count);
        }
    
    public class Info {
        private String name;
        private String introduction;
    
        private Info() {
    
        }
    
        private Info(String name, String introduction) {
            this.name = name;
            this.introduction = introduction;
        }
    
        public static Info of() {
            return of("default", "default");
        }
    
        public static Info of(String name, String introduction) {
            return new Info(name, introduction);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getIntroduction() {
            return introduction;
        }
    
        public void setIntroduction(String introduction) {
            this.introduction = introduction;
        }
    }
    
    public class test {
        public static void main(String[] args) {
            Info info = Info.of();
            Info info1 = Info.of("Alice", "a coder");
            System.out.println(info);
            System.out.println(info1);
        }
    }
    
    
  3. Builder模式

    public class Info {
        private String name;
        private String introduction;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getIntroduction() {
            return introduction;
        }
    
        public void setIntroduction(String introduction) {
            this.introduction = introduction;
        }
    
        @Override
        public String toString() {
            return "Info{name=" + name + "," +
                    "introduction = " + introduction +
                    "}";
        }
    
        //创建Builder类,注意方法返回类型,利用return this实现链式结构
        public static class InfoBuilder {
            private String name;
            private String introduction;
    
            public InfoBuilder name(String name) {
                this.name = name;
                return this;
            }
    
            public InfoBuilder introduction(String introduction) {
                this.introduction = introduction;
                return this;
            }
    
            //创建build方法,终止链式结构,并完成目标类的创建及赋值
            public Info build() {
                Info info = new Info();
                info.setName(this.name);
                info.setIntroduction(this.introduction);
                return info;
            }
        }
        
        //静态builder方法,用于创建Builder对象
        public static InfoBuilder builder () {
            return new InfoBuilder();
        }
    }
    
    public class test {
        public static void main(String[] args) {
            Info info = Info.builder()         //创建Builder对象
                    .name("Alice")
                    .introduction("a coder")   //相应参数赋值
                    .build();                  // 终止链式结构,返回目标类对象
            System.out.println(info);
        }
    }
    

使用lombok实现链式编程:

  1. return this

    使用注解@Accessors(chain = true)

    import lombok.Data;
    import lombok.experimental.Accessors;
    
    @Data
    @Accessors(chain = true)
    public class Info {
        private String name;
        private String introduction;
    }
    
    public class test {
        public static void main(String[] args) {
           Info info = new Info().setName("Alice").setIntroduction("a coder");
           System.out.println(info);
        }
    }
    //Info(name=Alice, introduction=a coder)
    
  2. 静态初始化方法of

    通过staticname指定静态方法为of

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.RequiredArgsConstructor;
    import lombok.experimental.Accessors;
    
    @Data
    @Accessors(chain = true)
    @RequiredArgsConstructor(staticName = "of")
    @AllArgsConstructor(staticName = "of")
    public class Info {
        private String name;
        private String introduction;
    }
    
    public class test {
        public static void main(String[] args) {
           Info info = Info.of("Alice", "a coder");
           System.out.println(info);
        }
    }
    
  3. Builder

    使用@Builder注解

    import lombok.Builder;
    import lombok.Data;
    
    @Data
    @Builder
    public class Info {
        private String name;
        private String introduction;
    }
    
    public class test {
        public static void main(String[] args) {
           Info info = Info.builder()
                   .name("Alice")
                   .introduction("a coder")
                   .build();
           System.out.println(info);
        }
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值