Chain Programming & Builder Pattern
Preface
I have written about the traditional builder pattern before.
Today I suddenly thought of: how to implement chain programming.
Then I discovered that chain programming and the creative builder pattern seem to be very similar.
I don’t even know what the difference is, so I’ll record it here. If I realize it one day, maybe it can help me figure it out.
时隔半年,对之前的遗留问题有了新的认识:
Builder pattern is a way to implement chain programming.
1: Whether it is builder mode or conventional chain programming. It's all about solving: getting a complex and troublesome object.
2: Whether it is builder mode or conventional chain programming, the implementation code of each complex object is one-time
(in order to piece together a unique data structure, so the reusability is very low)
Builder mode can also be implemented using lombook annotations: @Builder
提示:以下是本篇文章正文内容,下面案例可供参考
1. Traditional creative builder model
/**
* 手机类
*/
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
@Data
public class Phone {
private String cpu;
private String screen;
private String memory;
private String mainboard;
//私有构造方法
private Phone(Builder builder) {
this.cpu = builder.cpu;
this.screen = builder.screen;
this.memory = builder.memory;
this.mainboard = builder.mainBoard;
}
public static final class Builder {
private String cpu;
private String screen;
private String memory;
private String mainBoard;
public Builder cpu(String cpu) {
this.cpu = cpu;
return this;
}
public Builder screen(String screen) {
this.screen = screen;
return this;
}
public Builder memory(String memory) {
this.memory = memory;
return this;
}
public Builder mainBoard(String mainBoard) {
this.mainBoard = mainBoard;
return this;
}
//使用构建者创建Phone对象
public Phone build() {
return new Phone(this);
}
}
public static void main(String[] args) {
//创建手机对象 通过构建者对象获取手机对象
Phone phone = new Phone.Builder()
.cpu("intel")
.screen("三星屏幕")
.memory("金士顿内存条")
.mainBoard("华硕主板")
.build();
System.out.println(JSONObject.toJSONString(phone));
}
}
//{"cpu":"intel","mainboard":"华硕主板","memory":"金士顿内存条","screen":"三星屏幕"}
2. Chain programming - creating simple objects
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* 手机类
*/
@Data
@Accessors(chain = true)
public class Phone2 {
private String cpu;
private String screen;
private String memory;
private String mainBoard;
public static void main(String[] args) {
//创建手机对象 通过构建者对象获取手机对象
Phone2 phone = new Phone2()
.setCpu("intel")
.setScreen("三星屏幕")
.setMemory("金士顿内存条")
.setMainBoard("华硕主板");
System.out.println(JSONObject.toJSONString(phone));
}
}
//{"cpu":"intel","mainBoard":"华硕主板","memory":"金士顿内存条","screen":"三星屏幕"}
3. Chain programming - modify fields in objects
import com.alibaba.fastjson.JSONObject;
/**
* 链式编程:精髓在于 - 对象里的方法返回对象自身
* 例子:创建型建造者模式里是用了链式编程
* SpringBuilder里面使用了
*
* @author lobsterlong
* @date 2022/7/22
*/
public class StringB {
public String name;
public StringB(String name) {
this.name = name;
}
public StringB addPrefix(String prefix) {
this.name = prefix + this.name;
return this;
}
public StringB addSuffix(String suffix) {
this.name += suffix;
return this;
}
public static void main(String[] args) {
StringB b = new StringB("long");
System.out.println(JSONObject.toJSONString(b));
System.out.println(JSONObject.toJSONString(b.addPrefix("lobster.")));
System.out.println(JSONObject.toJSONString(b.addSuffix("@0722")));
}
}
//{"name":"long"}
//{"name":"lobster.long"}
//{"name":"lobster.long@0722"}
Summarize
Just from one and two perspectives, the final effect is the same.
The specific difference is not clear...