java中用builder模式实现实体类的创建
2015-11-16 23:39
706 查看
package com.panther.builder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式, * 所构建的对象往往需要多步初始化或赋值才能完成。 * 那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢? * 其中使用Builder模式来替代多参数构造函数是一个比较好的实践法则。 * Created by panther.dongdong on 2015/11/16. */ public class Student { private static final Logger LOGGER = LoggerFactory.getLogger(Student.class.getName()); private int id; private int age; private String name; private String sex; private String love; private String phone; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getLove() { return love; } public void setLove(String love) { this.love = love; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public static Builder create() { return new Builder(); } public Student(Builder builder) { this.id = builder.id; this.age = builder.age; this.name = builder.name; this.sex = builder.sex; this.love = builder.love; this.phone = builder.phone; } public static class Builder { private int id = 0; private int age = 0; private String name = null; private String sex = null; private String love = null; private String phone = null; //构建的步骤 public Builder addId(int id) { this.id = id; return this; } public Builder addAge(int age) { this.age = age; return this; } public Builder addName(String name) { this.name = name; return this; } public Builder addSex(String sex) { this.sex = sex; return this; } public Builder addLove(String love) { this.love = love; return this; } public Builder addPhone(String phone) { this.phone = phone; return this; } public Student build() { return new Student(this); } } @Override public String toString() { return "Student{" + "id=" + id + ", age=" + age + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", love='" + love + '\'' + ", phone='" + phone + '\'' + '}'; } /** * 测试生成Student * * @param args */ public static void main(String[] args) { Student student = Student.create().addAge(18).addName("panther").addLove("play") .addPhone("110").addSex("男").build(); LOGGER.info("{}", student.toString()); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序