皓月白熏 2022-08-26 11:56 采纳率: 82.1%
浏览 31
已结题

建造者优化传递参数,怎么才能不new那么一下?请教大家

关于建造者模式优化传参,我看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;
    }
}

展开全部

  • 写回答

2条回答 默认 最新

  • i余数 2022-08-26 14:52
    关注
    
    public class NCBuilder {
        private String ip;
        private int port;
    
        //私有构造方法
        private NCBuilder(Builder builder) {
            this.ip = builder.ip;
            this.port = builder.port;
        }
    
        public static Builder builder(){
            return new Builder();
        }
        public static final class Builder {
            private String ip;
            private int port;
    
            public Builder ip(String ip) {
                this.ip = ip;
                return this;
            }
    
            public Builder port(int port) {
                this.port = port;
                return this;
            }
    
            //使用构建者创建NCBuilder对象
            public NCBuilder build() {
                return new NCBuilder(this);
            }
    
        }
    
        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;
        }
        
    }
    

    使用

    NCBuilder ncBuilder =  NCBuilder.builder().ip("localhost").port(8080).build();
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 9月3日
  • 已采纳回答 8月27日
  • 修改了问题 8月26日
  • 创建了问题 8月26日

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部