一、链式编程定义

链式编程的原理就是返回一个this对象,就是返回本身,达到链式效果。

二、jdk中 StringBuffer 源码

我们经常用的 StringBuffer 就是 实现了链式的写法。

StringBuffer builder = new StringBuffer();

builder.append("blake").append("bob").append("alice").append("linese").append("eve");

是不是很方便呢!

怎么实现呢,其实就是在设置的 返回当前的对象 。

源码如下:

@Override

public StringBuilder append(String str) {

super.append(str);

return this;

}

三、按照上面的方法写一个例子

public class StudentBean {

private String name;

private int age;

public String getName() {

return name;

}

public StudentBean setName(String name) {

this.name = name;

return this;

}

public int getAge() {

return age;

}

public StudentBean setAge(int age) {

this.age = age;

return this;

}

}

测试:

public class Main {

public static void main(String[] args) {

StudentBean studentBean = new StudentBean().setAge(22).setName("ly");

System.out.println(studentBean.getAge());

System.out.println(studentBean.getName());

}

}

四、lombok 链式编程

其实,lombok 已经提供该 style,我们把这个bean 改成 lombok 实现只需要加上一个 @Accessors(chain = true) 即可。

@Accessors(chain = true)

@Getter

@Setter

public class StudentBean {

private String name;

private int age;

}

上面代码编译之后:

public class StudentBean {

private String name;

private int age;

public StudentBean() {

}

public String getName() {

return this.name;

}

public int getAge() {

return this.age;

}

public StudentBean setName(String name) {

this.name = name;

return this;

}

public StudentBean setAge(int age) {

this.age = age;

return this;

}

}

五、lombok 实现静态的链式编程

写StudentBean这个bean的时候,会有一些必输字段,比如StudentBean中的name字段,一般处理的方式是将name字段包装成一个构造方法,只有传入name这样的构造方法,才能创建一个StudentBean对象。

使用 lombok 将更改成如下写法: @RequiredArgsConstructor 和 @NonNull

@Accessors(chain = true)

@Getter

@Setter

@RequiredArgsConstructor(staticName = "of")

public class StudentBean {

@NonNull

private String name;

private int age;

}

上面代码编译之后:

public class StudentBean {

@NonNull

private String name;

private int age;

@NonNull

public String getName() {

return this.name;

}

public int getAge() {

return this.age;

}

public StudentBean setName(@NonNull String name) {

if (name == null) {

throw new NullPointerException("name");

} else {

this.name = name;

return this;

}

}

public StudentBean setAge(int age) {

this.age = age;

return this;

}

private StudentBean(@NonNull String name) {

if (name == null) {

throw new NullPointerException("name");

} else {

this.name = name;

}

}

public static StudentBean of(@NonNull String name) {

return new StudentBean(name);

}

}

测试方法:

public class Main {

public static void main(String[] args) {

StudentBean studentBean = StudentBean.of("zhangsan").setAge(22);

System.out.println(studentBean.getAge());

System.out.println(studentBean.getName());

}

}

这样不仅实现了链式编程,还实现了静态创建。

六、自定义 builder模式的链式Bean

build模式实现原理为在bean里面创建一个 静态builder方法 和一个 静态内部Builder类 ,通过调用静态builder方法来创建 Builder类,然后通过 builder类 中的 build方法直接创建一个Bean,具体如下:

public class StudentBean {

private String name;

private int age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public static Builder builder() {

return new Builder();

}

public static class Builder{

private String name;

private int age;

public Builder name(String name) {

this.name = name;

return this;

}

public Builder age(int age) {

this.age = age;

return this;

}

public StudentBean build() {

StudentBean studentBean = new StudentBean();

studentBean.setName(name);

studentBean.setAge(age);

return studentBean;

}

}

}

测试方法:

StudentBean studentBean = StudentBean.builder().name("zhangsan").age(11).build();

七、lombok 实现 builder模式的链式bean

这样就实现了一个builder模式的链式bean。其实用lombok就一个注解的事情,调用与上面同样

@Builder

public class StudentBean {

private String name;

private int age;

}

上面代码编译之后:

public class StudentBean {

private String name;

private int age;

StudentBean(String name, int age) {

this.name = name;

this.age = age;

}

public static StudentBean.StudentBeanBuilder builder() {

return new StudentBean.StudentBeanBuilder();

}

public static class StudentBeanBuilder {

private String name;

private int age;

StudentBeanBuilder() {

}

public StudentBean.StudentBeanBuilder name(String name) {

this.name = name;

return this;

}

public StudentBean.StudentBeanBuilder age(int age) {

this.age = age;

return this;

}

public StudentBean build() {

return new StudentBean(this.name, this.age);

}

public String toString() {

return "StudentBean.StudentBeanBuilder(name=" + this.name + ", age=" + this.age + ")";

}

}

}

可以发现添加了 @Builder 注解之后,自动生成了静态builder方法 和一个 静态内部Builder类,并没有生成 get、set 方法,我们在上面例子中加入 @Data和 @Accessors(chain = true)注解:

@Builder

@Data

@Accessors(chain = true)

public class StudentBean {

private String name;

private int age;

}

上面代码编译之后:

public class StudentBean {

private String name;

private int age;

StudentBean(String name, int age) {

this.name = name;

this.age = age;

}

public static StudentBean.StudentBeanBuilder builder() {

return new StudentBean.StudentBeanBuilder();

}

private StudentBean() {

}

public String getName() {

return this.name;

}

public int getAge() {

return this.age;

}

public StudentBean setName(String name) {

this.name = name;

return this;

}

public StudentBean setAge(int age) {

this.age = age;

return this;

}

public boolean equals(Object o) {

if (o == this) {

return true;

} else if (!(o instanceof StudentBean)) {

return false;

} else {

StudentBean other = (StudentBean)o;

if (!other.canEqual(this)) {

return false;

} else {

Object this$name = this.getName();

Object other$name = other.getName();

if (this$name == null) {

if (other$name == null) {

return this.getAge() == other.getAge();

}

} else if (this$name.equals(other$name)) {

return this.getAge() == other.getAge();

}

return false;

}

}

}

protected boolean canEqual(Object other) {

return other instanceof StudentBean;

}

public int hashCode() {

int PRIME = true;

int result = 1;

Object $name = this.getName();

int result = result * 59 + ($name == null ? 43 : $name.hashCode());

result = result * 59 + this.getAge();

return result;

}

public String toString() {

return "StudentBean(name=" + this.getName() + ", age=" + this.getAge() + ")";

}

public static class StudentBeanBuilder {

private String name;

private int age;

StudentBeanBuilder() {

}

public StudentBean.StudentBeanBuilder name(String name) {

this.name = name;

return this;

}

public StudentBean.StudentBeanBuilder age(int age) {

this.age = age;

return this;

}

public StudentBean build() {

return new StudentBean(this.name, this.age);

}

public String toString() {

return "StudentBean.StudentBeanBuilder(name=" + this.name + ", age=" + this.age + ")";

}

}

}

最终生成类如上面一样,属性的 get、set 方法有了,并且支持链式调用,而且还是builder模式。

八、 更多其他Lombok注解的使用

java链式编程_Java 链式编程 和 lombok 实现链式编程相关推荐

  1. java链式结构_java语言实现队列顺序结构与链式结构

    本文主要向大家介绍了java语言实现队列顺序结构与链式结构,通过具体的内容向大家展示,希望对大家学习java语言有所帮助. 队列的顺序存储结构实现 public class Queue{ privat ...

  2. java 流常用接口_java 8新特性5--使用集合流式API

    PS:向公众号发送关键字可以搜索文章哦! 使用集合的流式API 直接上代码了,常用api都有了,要点都在注释中 苹果类: packagejava8.stream;/***@authorqiang.xi ...

  3. java先进先出 循环队列_java队列--先进先出(循环队列、链队列)

    队列:只允许在一端进行插入操作(队尾),在另一端进行删除操作(队头). 队列的特征就是: 先进先出. 队列的思想及实现也同样非常简单.在生活中的各种常常都需要排队进行,键盘中缓存区.操作系统中的作业调 ...

  4. java get set 注解_java技能提升,用Lombok甩掉get和set,让代码变得更简洁

    前言 前几天有个新来的同事(实习生)惊讶的对我说:我们的代码里好多错误,我的程序本地都启动不了. 我一脸懵逼的质问他:目前线上的代码,怎么会有问题吗? 他不服气的说:你来看嘛,就是有问题,Dao实体g ...

  5. java链式编程_Java链式编程学习

    Java链式编程 在使用jquery时肯定对它的链式编程惊艳到,慢慢的其它语言这种编程模式也逐渐增多.其本身并不复杂,在调用方法时,方法最后返回对象本身,以达到链式编程的效果. 链式编程比较简单,只要 ...

  6. java链式编程_Java 中的链式编程

    前言 ​在写项目的时候,有一个实体类有好多个属性,new 出来之后需要不停的使用setXXX( )方法,效率低而且代码可读性差,查询了下发现可以实现实体类的链式编程. public class Use ...

  7. Java8链式调用_java链式调用(转载http://www.cnblogs.com/quiet-snowy-day/p/6091233.html)

    记录最近在项目设计中遇到的一个小问题. 前提:有这样两个POJO类,它们都可以通过链式调用的方式来设置其属性值,其中一个类继承了另一个类. 问题:通过链式调用,子类对象访问父类方法后,如何使返回对象仍 ...

  8. java函数式编程_Java 函数式编程和 lambda 表达式详解

    作者:DemonsI my.oschina.net/demons99/blog/2223079 为什么要使用函数式编程 函数式编程更多时候是一种编程的思维方式,是种方法论.函数式与命令式编程的区别主要 ...

  9. android xml java混合编程_Java学习中注解与多线程,网络编程与XML技术

    本部分内容主要有集合框架及泛型,实用类,输入和输出处理,注解与多线程,网络编程与XML技术.初次学习这部分会感觉很难,主要是概念难于理解,最好是多看看例子,多练习.下面是个人的总结 拉勾IT课小编为大 ...

  10. Java for函数用法_Java函数式编程(四)集合的使用

    第二章:集合的使用 我们经常会用到各种集合,数字的,字符串的还有对象的.它们无处不在,哪怕操作集合的代码要能稍微优化一点,都能让代码清晰很多.在这章中,我们探索下如何使用lambda表达式来操作集合. ...

最新文章

  1. 多线程threading
  2. Spring使用环境变量控制配置文件加载(转)
  3. vb.net 正则 替换 第n个_Python中正则表达式模块详解
  4. MVP架构设计 进阶三
  5. 谷歌利用OKR系统考核 脑力行业或可借鉴
  6. [20190214]11g Query Result Cache RC Latches.txt
  7. Leet Code OJ 70. Climbing Stairs [Difficulty: Easy]
  8. 转:70个漂亮实用的JavaScript和Ajax技术(有图有例子)
  9. 《程序员面试金典》+《算法导论》
  10. antd select option 设置字体颜色_匹配颜色是照片合成重要关键点
  11. Chrome 扩展程序 CrxMouse Techzero优化版 更新至 v3.0.4
  12. 《卓有成效的管理者》读书笔记(二)——卓有成效是可以学会的
  13. VS2019:添加现有项目 / 现有cpp文件
  14. 可视化——Excel2进阶
  15. 企微有客户流失提醒功能吗?如何设置?
  16. 适合送女朋友的情人节礼物?畅销火热的好物分享
  17. Java byte[]与short[]之间转换
  18. nginx反向代理非80端口/nginx反代非80端口
  19. 210学习日记(18)_ARM基础知识
  20. 快速入门 | 篇十三:正运动技术运动控制器ZDevelop 编程软件的使用

热门文章

  1. android udp 收发例子_如何利用光衰减器来测试光纤收发器的灵敏度?
  2. mybatis中like模糊查询的几种写法及注意点
  3. python image模块安装_python之PIL库(Image模块)
  4. python一箭穿心代码怎样复制,Python Decimal copy_sign()用法及代码示例
  5. Git config 查看和设置配置信息
  6. ngrinder监控资源monitor
  7. make menuconfig错误
  8. python在实际中的作用_Python面向对象中__init__的实际作用是什么?
  9. 计算机在中职教育中的运用论文,中职计算机教育的相关论文(2)
  10. java 一组数据中偏差较大的数据_深入分析数据结构中的队列(java代码实现)