Java重构类具有相同的方法

发布于 2025-01-27 01:36:25 字数 883 浏览 1 评论 0原文

我对重构类的任何建议,所有静态方法都是相同的,只有一个变量代码是不同的。

public class SuccessResponseBuilder {

    static ResponseCode code = ResponseCode.OK;

    public static <T> @NotNull ResponseBean build() {
        return ResponseBean.builder(code, null);
    }

    public static <T> ResponseBean build(T data) {
        return ResponseBean.builder(code, data);
    }
}
public class ErrorResponseBuilder {

    static ResponseCode code = ResponseCode.ERROR;

    public static <T> @NotNull ResponseBean build() {
        return ResponseBean.builder(code, null);
    }

    public static <T> ResponseBean build(T data) {
        return ResponseBean.builder(code, data);
    }
}

客户端将使用这种方式获取结果

errorresponsebuilder.build(e.getMessage()); successresponsebuilder.build(“ ok”);

Any suggestions for me to refactor the class, all the static methods are the same, only one of the variable code is different.

public class SuccessResponseBuilder {

    static ResponseCode code = ResponseCode.OK;

    public static <T> @NotNull ResponseBean build() {
        return ResponseBean.builder(code, null);
    }

    public static <T> ResponseBean build(T data) {
        return ResponseBean.builder(code, data);
    }
}
public class ErrorResponseBuilder {

    static ResponseCode code = ResponseCode.ERROR;

    public static <T> @NotNull ResponseBean build() {
        return ResponseBean.builder(code, null);
    }

    public static <T> ResponseBean build(T data) {
        return ResponseBean.builder(code, data);
    }
}

The client will used this way to get the result

ErrorResponseBuilder.build(e.getMessage());
SuccessResponseBuilder.build("ok");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2025-02-03 01:36:25

这是一种灵活,干净的方法。主要想法是创建内部Builder类。优势是您可以简单地添加一个新的wendersecode而无需创建新类。

public class ResponseBean {

    private final ResponseCode code;
    private Object data;

    private ResponseBean(ResponseCode code) {
        this.code = code;
    }

    private ResponseBean(ResponseCode code, Object data) {
        this.code = code;
        this.data = data;
    }

    public static Builder ok() {
        return new Builder(ResponseCode.OK);
    }

    public static Builder error() {
        return new Builder(ResponseCode.ERROR);
    }

    /* if you would like to create a new ResponseCode:
    public static Builder yourNewCode() {
        return new Builder(ResponseCode.NEW_CODE);
    }
    */

    public static class Builder {

        private final ResponseCode code;

        public Builder(ResponseCode code) {
            this.code = code;
        }
        
        public ResponseBean build() {
            return new ResponseBean(code);
        }

        public ResponseBean build(Object data) {
            return new ResponseBean(code, data);
        }
    }
}

用法:

public void print() {
    ResponseBean okResponse = ResponseBean.ok().build("This is ok data");
    ResponseBean okResponseWithoutData = ResponseBean.ok().build();
    ResponseBean errorResponse = ResponseBean.error().build("This is error data");
    ResponseBean errorResponseWithoutData = ResponseBean.error().build();

    System.out.println(okResponse);
    System.out.println(okResponseWithoutData);
    System.out.println(errorResponse);
    System.out.println(errorResponseWithoutData);
}

输出将是

ResponseBean(code=OK, data=This is ok data)
ResponseBean(code=OK, data=null)
ResponseBean(code=ERROR, data=This is error data)
ResponseBean(code=ERROR, data=null)

Here is a flexible, clean approach. The main idea is creating an inner Builder class. The advantage is you can simply add a new ResponseCode without creating new class.

public class ResponseBean {

    private final ResponseCode code;
    private Object data;

    private ResponseBean(ResponseCode code) {
        this.code = code;
    }

    private ResponseBean(ResponseCode code, Object data) {
        this.code = code;
        this.data = data;
    }

    public static Builder ok() {
        return new Builder(ResponseCode.OK);
    }

    public static Builder error() {
        return new Builder(ResponseCode.ERROR);
    }

    /* if you would like to create a new ResponseCode:
    public static Builder yourNewCode() {
        return new Builder(ResponseCode.NEW_CODE);
    }
    */

    public static class Builder {

        private final ResponseCode code;

        public Builder(ResponseCode code) {
            this.code = code;
        }
        
        public ResponseBean build() {
            return new ResponseBean(code);
        }

        public ResponseBean build(Object data) {
            return new ResponseBean(code, data);
        }
    }
}

Usage:

public void print() {
    ResponseBean okResponse = ResponseBean.ok().build("This is ok data");
    ResponseBean okResponseWithoutData = ResponseBean.ok().build();
    ResponseBean errorResponse = ResponseBean.error().build("This is error data");
    ResponseBean errorResponseWithoutData = ResponseBean.error().build();

    System.out.println(okResponse);
    System.out.println(okResponseWithoutData);
    System.out.println(errorResponse);
    System.out.println(errorResponseWithoutData);
}

Output will be

ResponseBean(code=OK, data=This is ok data)
ResponseBean(code=OK, data=null)
ResponseBean(code=ERROR, data=This is error data)
ResponseBean(code=ERROR, data=null)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文