Build模式
来源:互联网 发布:网络剧余罪 编辑:程序博客网 时间:2024/06/04 01:01
Builder模式,意思是我们new 对象的时候附带构造方法,如果里面的成员变量太多的话,我们根本不知道他是代表什么意思,由此我们可引入静态内部类类Builder类,可阅读性就明显提高了。
下面是实体类的构造方式,加入build类
public class UserDTO { private long id; private String userName; private String loginName; private String sex; private double height; private double weight; public UserDTO() { } public UserDTO(long id, String userName, String loginName, String sex, double height, double weight) { this.id = id; this.userName = userName; this.loginName = loginName; this.sex = sex; this.height = height; this.weight = weight; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } private UserDTO(Builder builder) { this.id = builder.id; this.userName = builder.userName; this.loginName = builder.loginName; this.sex = builder.sex; this.height = builder.height; this.weight = builder.weight; } //静态内部Builder类 static class Builder { private long id; private String userName; private String loginName; private String sex; private double height; private double weight; public Builder setId(long id) { this.id = id; return this; } public Builder setUserName(String userName) { this.userName = userName; return this; } public Builder setLoginName(String loginName) { this.loginName = loginName; return this; } public Builder setSex(String sex) { this.sex = sex; return this; } public Builder setHeight(double height) { this.height = height; return this; } public Builder setWeight(double weight) { this.weight = weight; return this; } public UserDTO build() { return new UserDTO(this); } }}
这个时候 我们在其他地方new对象的时候就是这样子的
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); UserDTO.Builder builder = new UserDTO.Builder(); UserDTO userDTO = builder .setId(1L) .setUserName("user") .setLoginName("admin") .setHeight(170.00) .setWeight(60.00) .setSex("男") .build(); }}
这样子 每个成员变量是不是都比较清晰了,像OKkttp的构造方法也是类似的这个的。
当然我们也可以直接使用
AA aa=new AA();
aa.setXX(XX);这个方法了
阅读全文
0 0
- build 模式
- Build模式
- Build模式
- 生成器模式(Build)
- 生成器模式(Build)
- Java build模式
- android build 模式
- 简单的build模式
- build模式小例子
- java build设计模式
- Build设计模式
- JAVA设计模式 Build 模式
- MVP模式+Build模式+Observe模式
- Java的建造模式(Build)
- 建造者(build)模式
- JavaBean构造使用Build模式
- build模式的第二篇
- 简单模拟Build设计模式
- hihocoder 1236 (分块+bitset乱搞)
- multiparty node.js的中间件 --- 上传文件
- 【DB】MySQL中case when then 的使用
- 学习阿里巴巴开发手册-1
- python基础--list操作
- Build模式
- to_timestamp、to_timestamp、to_date 函数
- php正则表达式使用的详细介绍
- 柜
- 自定义字段的设计与实现(Java实用版)
- noe4j之cypher之where
- 1028. 人口普查(20)
- Appium Timeout参数设置
- ajax请求时提醒参数为空的解决办法