非解耦方式
package com.finger.test.designpattern.DBuilderPattern;
/**
* @Des:
* @Auther: 飛狐
* @Date: 2019/3/3
*/
public class TTeacherInfo {
private String userName;
private String password;
private Integer age;
private Integer sex;
private String address;
public TTeacherInfo(TTeacherBuilder builder) {
this.userName = builder.userName;
this.password = builder.password;
this.age = builder.age;
this.sex = builder.sex;
this.address = builder.address;
}
@Override
public String toString() {
return "TTeacherInfo{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
", age=" + age +
", sex=" + sex +
", address='" + address + '\'' +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public static class TTeacherBuilder{
private String userName;
private String password;
private Integer age;
private Integer sex;
private String address;
public TTeacherBuilder(String userName,String password){
this.userName = userName;
this.password = password;
}
public TTeacherBuilder age(Integer age){
this.age = age;
return this;
}
public TTeacherBuilder sex(Integer sex){
this.sex = sex;
return this;
}
public TTeacherBuilder addrees(String address){
this.address = address;
return this;
}
public TTeacherInfo build(){
return new TTeacherInfo(this);
}
}
public static void main(String[] args) {
TTeacherInfo teacherInfo = new TTeacherBuilder("哈哈","123456").sex(1).build();
System.out.println(teacherInfo.toString());
}
}
解耦方式
package com.finger.test.designpattern.DBuilderPattern;
/**
* @Des:
* @Auther: 飛狐
* @Date: 2019/3/3
*/
public class TUserInfo {
private String userName;
private String password;
private Integer age;
private Integer sex;
private String address;
@Override
public String toString() {
return "TUserInfo{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
", age=" + age +
", sex=" + sex +
", address='" + address + '\'' +
'}';
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.finger.test.designpattern.DBuilderPattern;
import com.finger.test.pojo.TUser;
/**
* @Des:
* @Auther: 飛狐
* @Date: 2019/3/3
*/
public class UserInfoBuilder {
private TUserInfo userInfo;
public UserInfoBuilder(String userName,String password){
userInfo = new TUserInfo();
userInfo.setUserName(userName);
userInfo.setPassword(password);
}
public UserInfoBuilder age(Integer age){
userInfo.setAge(age);
return this;
}
public UserInfoBuilder sex(Integer sex){
userInfo.setSex(sex);
return this;
}
public UserInfoBuilder address(String address){
userInfo.setAddress(address);
return this;
}
public TUserInfo build(){
return userInfo;
}
}
驗證:
package com.finger.test.designpattern.DBuilderPattern;
/**
* @Des:
* @Auther: 飛狐
* @Date: 2019/3/3
*/
public class UserInfoBuilderTest {
public static void main(String[] args) {
TUserInfo userInfo = new UserInfoBuilder("李雷","123456").address("晉南").build();
System.out.println(userInfo.toString());
}
}
如果覺得我的文章對您有用,請隨意打賞。您的支援將鼓勵我繼續創作!
打賞支援
暫無回覆。