模式简介
build设计模式是Java开发中常用的一种实例化对象的设计模式,在谷歌的guava和rabbitmq的开发中用到了很多。Java Builder模式主要是用一个内部类去实例化一个对象,避免一个类出现过多构造函数,而且构造函数如果出现默认参数的话,很容易出错。build设计模式类似于通过一个代理来构建对象,可以对对象起到更好的封装作用。
应用场景
1.实例化对象时需要传入的参数过多,而且并不是所有的参数都是我们需要的,如果采用构造方法或是set方法传参数的话,代码重复很高,且不利于维护。
2.当有的参数作为策略是可选时。
代码(别的都是废话,技术本人认为最好的学习方式就是看代码)
package rabbitmq58lib.model;
import java.util.concurrent.TimeUnit;
/**
* Created with IntelliJ IDEA.
* Description:
* User: weicaijia
* Date: 2017-08-28
* Time: 14:37
*/
public class RabbitExchangeModel {
private String ServerUrl;
private int Port;
private String UserName;
private String Pwd;
private String QueueName;
private String ExchangeName;
private String Type;
private String ConsumerKey;
private String ProducerKey;
private boolean isDLX;
private String DLE;
private String DLK;
private TimeUnit retryTimeUnit;
private long retryDelaytime;
private int retries;
private boolean isRetried;
private RabbitExchangeModel(Builder builder) {
this.ServerUrl = builder.ServerUrl;
this.Port = builder.Port;
this.UserName = builder.UserName;
this.Pwd = builder.Pwd;
this.QueueName = builder.QueueName;
this.ExchangeName = builder.ExchangeName;
this.Type = builder.Type;
this.ConsumerKey = builder.ConsumerKey;
this.ProducerKey = builder.ProducerKey;
this.isDLX = builder.isDLX;
this.DLE = builder.DLE;
this.DLK = builder.DLK;
this.retryTimeUnit = builder.retryTimeUnit;
this.retryDelaytime = builder.retryDelaytime;
this.retries = builder.retries;
this.isRetried = builder.isRetried;
}
public static class Builder {
private String ServerUrl;
private int Port;
private String UserName;
private String Pwd;
private String QueueName;
private String ExchangeName;
private String Type;
private String ConsumerKey;
private String ProducerKey;
private boolean isDLX;
private String DLE;
private String DLK;
private TimeUnit retryTimeUnit;
private long retryDelaytime;
private int retries;
private boolean isRetried;
private boolean Durable = true;
public Builder(String ServerUrl, int Port, String UserName, String Pwd, String ExchangeName, String Type) {
this.ServerUrl = ServerUrl;
this.Port = Port;
this.UserName = UserName;
this.Pwd = Pwd;
this.ExchangeName = ExchangeName;
this.Type = Type;
}
public Builder QueueName(String QueueName) {
this.QueueName = QueueName;
return this;
}
public Builder ConsumerKey(String ConsumerKey) {
this.ConsumerKey = ConsumerKey;
return this;
}
public Builder ProducerKey(String ProducerKey) {
this.ProducerKey = ProducerKey;
return this;
}
public Builder isDLX(boolean isDLX) {
this.isDLX = isDLX;
return this;
}
public Builder DLE(String DLE) {
this.DLE = DLE;
return this;
}
public Builder DLK(String DLK) {
this.DLK = DLK;
return this;
}
public Builder retryTimeUnit(TimeUnit retryTimeUnit) {
this.retryTimeUnit = retryTimeUnit;
return this;
}
public Builder retryDelaytime(long retryDelaytime) {
this.retryDelaytime = retryDelaytime;
return this;
}
public Builder retries(int retries) {
this.retries = retries;
return this;
}
public Builder isRetried(boolean isRetried) {
this.isRetried = isRetried;
return this;
}
public Builder Durable(boolean Durable) {
this.Durable = true;
return this;
}
public RabbitExchangeModel build() {
return new RabbitExchangeModel(this);
}
}
public String getServerUrl() {
return ServerUrl;
}
public int getPort() {
return Port;
}
public String getUserName() {
return UserName;
}
public String getPwd() {
return Pwd;
}
public String getQueueName() {
return QueueName;
}
public String getExchangeName() {
return ExchangeName;
}
public String getType() {
return Type;
}
public String getConsumerKey() {
return ConsumerKey;
}
public String getProducerKey() {
return ProducerKey;
}
public boolean isDLX() {
return isDLX;
}
public String getDLE() {
return DLE;
}
public String getDLK() {
return DLK;
}
public TimeUnit getRetryTimeUnit() {
return retryTimeUnit;
}
public long getRetryDelaytime() {
return retryDelaytime;
}
public int getRetries() {
return retries;
}
public boolean isRetried() {
return isRetried;
}
}
弊端
目前该代码存在一个问题,就是对已经创建的对象的修改的问题,当然我们可以通过加set方法去实现。
ps:附上一篇build精华帖,转载自:java设计模式之建造者模式