# 第5节.Nacos中建造者设计模式的使用
# 一、建造者模式的定义
将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。
# 二、建造模式的使用场景
当一个类的构造函数参数个数超过4个,而且这些参数有些是可选的参数,考虑使用构造者模式。
建造者模式一般是链式调用。
# 三、Nacos中的使用位置
# 3.1、实例元数据信息构造
nacos中的注册实例的接口,对于接收到的实例信息,采用了InstanceBuilder这个类完成构造,避免这个定义多个构造函数导致的需求变化问题,InstanceBuilder类的使用代码如下所示:
public String register(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId,
@RequestParam String serviceName, @RequestParam String ip,
@RequestParam(defaultValue = UtilsAndCommons.DEFAULT_CLUSTER_NAME) String cluster,
@RequestParam Integer port, @RequestParam(defaultValue = "true") Boolean healthy,
@RequestParam(defaultValue = "1") Double weight, @RequestParam(defaultValue = "true") Boolean enabled,
@RequestParam String metadata, @RequestParam Boolean ephemeral) throws Exception {
//
NamingUtils.checkServiceNameFormat(serviceName);
checkWeight(weight);
// 使用构造者设计模式建造实例对象
final Instance instance = InstanceBuilder.newBuilder().setServiceName(serviceName).setIp(ip)
.setClusterName(cluster).setPort(port).setHealthy(healthy).setWeight(weight).setEnabled(enabled)
.setMetadata(UtilsAndCommons.parseMetadata(metadata)).setEphemeral(ephemeral).build();
if (ephemeral == null) {
instance.setEphemeral((switchDomain.isDefaultInstanceEphemeral()));
}
instanceServiceV2.registerInstance(namespaceId, serviceName, instance);
return "ok";
}
其中,InstanceBuilder类的代码如下,并采用了非内部类的方式进行定义:
public class InstanceBuilder {
private String instanceId;
private String ip;
private Integer port;
private Double weight;
private Boolean healthy;
private Boolean enabled;
private Boolean ephemeral;
private String clusterName;
private String serviceName;
private Map<String, String> metadata = new HashMap<>();
private InstanceBuilder() {
}
public InstanceBuilder setInstanceId(String instanceId) {
this.instanceId = instanceId;
return this;
}
public InstanceBuilder setIp(String ip) {
this.ip = ip;
return this;
}
public InstanceBuilder setPort(Integer port) {
this.port = port;
return this;
}
public InstanceBuilder setWeight(Double weight) {
this.weight = weight;
return this;
}
public InstanceBuilder setHealthy(Boolean healthy) {
this.healthy = healthy;
return this;
}
public InstanceBuilder setEnabled(Boolean enabled) {
this.enabled = enabled;
return this;
}
public InstanceBuilder setEphemeral(Boolean ephemeral) {
this.ephemeral = ephemeral;
return this;
}
public InstanceBuilder setClusterName(String clusterName) {
this.clusterName = clusterName;
return this;
}
public InstanceBuilder setServiceName(String serviceName) {
this.serviceName = serviceName;
return this;
}
public InstanceBuilder setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
return this;
}
public InstanceBuilder addMetadata(String metaKey, String metaValue) {
this.metadata.put(metaKey, metaValue);
return this;
}
/**
* Build a new {@link Instance}.
*
* @return new instance
*/
public Instance build() {
Instance result = new Instance();
if (!Objects.isNull(instanceId)) {
result.setInstanceId(instanceId);
}
if (!Objects.isNull(ip)) {
result.setIp(ip);
}
if (!Objects.isNull(port)) {
result.setPort(port);
}
if (!Objects.isNull(weight)) {
result.setWeight(weight);
}
if (!Objects.isNull(healthy)) {
result.setHealthy(healthy);
}
if (!Objects.isNull(enabled)) {
result.setEnabled(enabled);
}
if (!Objects.isNull(ephemeral)) {
result.setEphemeral(ephemeral);
}
if (!Objects.isNull(clusterName)) {
result.setClusterName(clusterName);
}
if (!Objects.isNull(serviceName)) {
result.setServiceName(serviceName);
}
result.setMetadata(metadata);
return result;
}
public static InstanceBuilder newBuilder() {
return new InstanceBuilder();
}
}
通过这个方法,可以看到将对象的构造过程封装为了单独的builder类。