链式编程:将多个操作(多行代码)使用点语法(.)链接成一行代码,增强代码可读性。
条件:方法调用时每次返回方法本身。
参考链接:Java链式编程学习 - 简书 (jianshu.com)
实现静态编程的三种方式:
-
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; }
-
静态初始化方法
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); } }
-
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实现链式编程:
-
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)
-
静态初始化方法
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); } }
-
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); } }