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模式构建一个对象】

    推荐阅读