java|java builder模式构建一个对象
参考:https://www.cnblogs.com/rolayblog/p/10552085.html
问题:基于什么样的场景才去使用这样的模式,带来什么样的好处?
用到的设计模式:构建者模式;意图:将一个复杂对象的构建和表示分离,更清晰明了;
@Data
public class Hobby {
private String name;
private String type;
private List logs;
}@Data
public class BuilderDesign {
private String username;
private Integer age;
private String password;
private List hobbies;
private BuilderDesign(String username, Integer age, String password, List hobbies) {
this.username = username;
this.age = age;
this.password = password;
this.hobbies = hobbies;
}public static class Builder {
private String username;
private Integer age;
private String password;
private List hobbies;
public BuilderDesign.Builder username(String username) {
this.username = username;
return this;
}public BuilderDesign.Builder age(Integer age) {
this.age = age;
return this;
}public BuilderDesign.Builder password(String password) {
this.password = password;
return this;
}public BuilderDesign.Builder hobbies(List hobbies) {
this.hobbies = hobbies;
return this;
}public BuilderDesign build() {
return new BuilderDesign(username, age, password, hobbies);
}
}
}
【java|java builder模式构建一个对象】
推荐阅读
- javaWeb项目之新闻管理系统详细开发过程
- 腾讯云Java工程师一面 + 被捞一面 + 二面面经(附答案)
- JavaScript删除有序数组中的重复项
- Java|Java 超详细讲解设计模式之中的抽象工厂模式
- JavaWeb学习记录——servlet+tomcat+request+respond+JSP实战项目(使用MVC模式开发)
- java|7个小技巧,老板再也无法留我加班了...
- C++设计模式|C++设计模式 - 访问器模式(Visitor)
- 浅析Java反射--Java
- JAVA|JAVA module-info.java文件详解
- [Java编程思想]|[Java编程思想] 第七章 复用类