Search code examples
javarefactoring

Java Refactor classes have same method


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");


Solution

  • 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)