建造者模式属于创建型模式中的一种,我个人的理解是建造者模式是为了让开发者写出更优雅的代码。一般来说,创建一个对象,我们都是通过new关键字来创建,但是对应一些属性特别多的类,而且属性的类型又是很多都是相近的这种,我们在通过构造函数new对象的时候,很容易出现对象值赋值错误的问题。另外通过new对象,也不是很优雅,所以我们就不太希望别人通过new来创建对象。这种情况我们就可以使用建造者模式来处理,既能避免赋值错误,也能写出看起来很优雅的代码。如何实现呢?直接看源码
package com.study.rabbitmq.designPattern.Builder;
import java.util.Arrays;
import java.util.List;
/**
* @Author alen
* @DATE 2022/7/5 22:23
* 建造者模式案例
*/
public class User {
String name;
int age;
String mobile;
String address;
String nickName;
String level;
String headImage;
int height;
String lastLoginTime;
List<String> goodsList;
private User(String name, int age, String mobile, String address, String nickName, String level, String headImage, int height, String lastLoginTime, List<String> goodsList) {
this.name = name;
this.age = age;
this.mobile = mobile;
this.address = address;
this.nickName = nickName;
this.level = level;
this.headImage = headImage;
this.height = height;
this.lastLoginTime = lastLoginTime;
this.goodsList = goodsList;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", mobile='" + mobile + '\'' +
", address='" + address + '\'' +
", nickName='" + nickName + '\'' +
", level='" + level + '\'' +
", headImage='" + headImage + '\'' +
", height=" + height +
", lastLoginTime='" + lastLoginTime + '\'' +
", goodsList=" + goodsList +
'}';
}
public static UserBuilder builder() {
return new UserBuilder();
}
//创建一个内部类
public static class UserBuilder {
private String name;
private int age;
private String mobile;
private String address;
private String nickName;
private String level;
private String headImage;
private int height;
private String lastLoginTime;
private List<String> goodsList;
public UserBuilder name(String name) {
this.name = name;
return this;
}
public UserBuilder age(int age) {
this.age = age;
return this;
}
public UserBuilder mobile(String mobile) {
this.mobile = mobile;
return this;
}
public UserBuilder address(String address) {
this.address = address;
return this;
}
public UserBuilder nickName(String nickName) {
this.nickName = nickName;
return this;
}
public UserBuilder level(String level) {
this.level = level;
return this;
}
public UserBuilder headImage(String headImage) {
this.headImage = headImage;
return this;
}
public UserBuilder height(int height) {
this.height = height;
return this;
}
public UserBuilder lastLoginTime(String lastLoginTime) {
this.lastLoginTime = lastLoginTime;
return this;
}
public UserBuilder goodsList(String... goodsList) {
this.goodsList = Arrays.asList(goodsList);
return this;
}
public User build() {
return new User(name, age, mobile, address, nickName, level, headImage, height, lastLoginTime, goodsList);
}
}
}
处理思路是在目标类中创建一个静态内部类,内部类中属性和目标类一样,并在内部类中提供一个方法builder(),内部类中提供属性赋值的方法,并在内部类中提供提一个build()方法对外响应目标类的对象。由于我们将构造函数私有化了,外部不能使用new来创建对象而使用build()来创建对象了,测试代码如下
package com.study.rabbitmq.designPattern;
import com.study.rabbitmq.designPattern.Builder.User;
/**
* @Author alen
* @DATE 2022/7/5 21:38
*/
public class Main {
public static void main(String[] args) {
User user = User.builder()
.name("张三")
.age(25)
.mobile("15988886666")
.address("上海市浦东新区")
.nickName("老张")
.level("A类")
.headImage("https://www.baidu.com")
.height(175)
.lastLoginTime("2022-07-05 22:45:02")
.goodsList("Iphone 13", "IMac", "IPod").build();
System.out.println(user);
}
}
使用建造者模式创建对象的过程中,我们就不大可能会出现赋值错误的问题了,创建对象也更加的优雅美观。建造者模式的学习分享就到这。。。