关于建造者模式优化传参,我看lombok是这样的
@Builder
@Data
public class TestBuilder {
private String name;
private String value;
}
使用
TestBuilder.builder().****.build();
我自己写的,有点问题,就是每次都要先new 一下,请问大家怎么改进。
NCBuilder ncBuilder = new NCBuilder.Builder().***.build();
附上建造者类型
public class NCBuilder {
private Command cmd;
private WebClient client;
private String version;
private JsonObject ncAcctContext;
private String ncUrl;
private String ip;
private int port;
private boolean isAsyn;
private String protocol;
//私有构造方法
private NCBuilder(Builder builder){
this.cmd = builder.cmd;
this.client=builder.client;
this.version=builder.version;
this.ncAcctContext=builder.ncAcctContext;
this.ncUrl=builder.ncUrl;
this.ip=builder.ip;
this.port=builder.port;
this.isAsyn=builder.isAsyn;
this.protocol=builder.protocol;
}
public static final class Builder {
private Command cmd;
private WebClient client;
private String version;
private JsonObject ncAcctContext;
private String ncUrl;
private String ip;
private int port;
private boolean isAsyn;
private String protocol;
public Builder cmd(Command cmd) {
this.cmd = cmd;
return this;
}
public Builder client(WebClient client) {
this.client = client;
return this;
}
public Builder version(String version) {
this.version = version;
return this;
}
public Builder ncAcctContext(JsonObject ncAcctContext) {
this.ncAcctContext = ncAcctContext;
return this;
}
public Builder ncUrl(String ncUrl) {
this.ncUrl = ncUrl;
return this;
}
public Builder ip(String ip) {
this.ip = ip;
return this;
}
public Builder port(int port) {
this.port = port;
return this;
}
public Builder isAsyn(boolean isAsyn) {
this.isAsyn = isAsyn;
return this;
}
public Builder protocol(String protocol) {
this.protocol = protocol;
return this;
}
//使用构建者创建NCBuilder对象
public NCBuilder build() {
return new NCBuilder(this);
}
}
public Command getCmd() {
return cmd;
}
public void setCmd(Command cmd) {
this.cmd = cmd;
}
public WebClient getClient() {
return client;
}
public void setClient(WebClient client) {
this.client = client;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public JsonObject getNcAcctContext() {
return ncAcctContext;
}
public void setNcAcctContext(JsonObject ncAcctContext) {
this.ncAcctContext = ncAcctContext;
}
public String getNcUrl() {
return ncUrl;
}
public void setNcUrl(String ncUrl) {
this.ncUrl = ncUrl;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public boolean isAsyn() {
return isAsyn;
}
public void setAsyn(boolean asyn) {
isAsyn = asyn;
}
public String getProtocol() {
return protocol;
}
public void setProtocol(String protocol) {
this.protocol = protocol;
}
}