更改函数返回类型的手动重构步骤是什么

问题描述 投票:0回答:1

更改函数返回类型的手动重构步骤是什么。 我从 Web 服务端点中提取了一个方法,然后又提取了一个方法对象,以将业务逻辑与 Web 机制分开。但我仍然有一个 Response 的返回类型。 我希望将业务逻辑与 Web 功能完全分离。

public class BusinessLogic {
    private boolean flag = true;
    
    public BusinessLogic(String payload) {
        flag = Boolean.parseBoolean(payload);
    }

    /* 
      The problem with the doLogic method is that the network logic of the response is mixed up in the business logic.
      Changing the return value allows us to separate the concerns.
     */
    
    public JavaxWsResponse doLogic() {
        if (flag) {
            return JavaxWsResponse.build("Success");
        }
        else {
            return JavaxWsResponse.build("Failure");
        }
    }
refactoring
1个回答
0
投票

您必须能够从旧的返回值映射到新的返回值,然后再映射回来,这样重构才能起作用。

  1. 将原始函数体提取到新函数中。大部分工作将在这个新功能上完成。
    public JavaxWsResponse doLogic() {
        return newMethod();
    }
  1. 创建新的返回类型(如果尚不存在)。
public enum BusinessResult {
    OK, FAIL;
}
  1. 为新的返回类型创建一个 Holder 类。一个带有公共字段的简单类就足够了。预计该类不会在重构结束后继续存在。 它将用作输出参数以保持代码编译和运行。
public class ReturnHolder {
    public BusinessResult result;
}
  1. 将 Holder 类作为参数添加到新函数中。
    public JavaxWsResponse newMethod(ReturnHolder ret) {
        if (flag) {
            return JavaxWsResponse.build("Success");
        }
        else {
            return JavaxWsResponse.build("Failure");
        }
    }
  1. 在新函数中,每个地方都有一个return语句,将holder类的字段设置为新返回类型的实例。
    public JavaxWsResponse newMethod(ReturnHolder ret) {
        if (flag) {
            ret.result = BusinessResult.OK;         // change 1
            return JavaxWsResponse.build("Success");
        }
        else {
            ret.result = BusinessResult.FAIL;       // change 2
            return JavaxWsResponse.build("Failure");
        }
    }
  1. 编写一个从新返回类型到旧返回类型的映射函数。 虽然将此函数放在新的返回类型上可能有意义,但如果进行重构的原因之一是将业务逻辑与传输层隔离 这可能并不理想。映射函数也可以放在调用者中。
    public static JavaxWsResponse transmute(BusinessResult businessResult) {
        switch (businessResult) {
        case OK:
            return  JavaxWsResponse.build("Success");
        case FAIL:
            return JavaxWsResponse.build("Failure");
        default:
            // I review of the code should show that this case is exceptional. but the compiler is picky.
            return JavaxWsResponse.build("Unexpected Failure");         
        }
    }
  1. 在旧函数中,对Holder类的内容使用映射函数以返回正确的类型。 新函数的当前返回值将被忽略。
    public JavaxWsResponse doLogic() {
        ReturnHolder ret = new ReturnHolder();
        newMethod(ret);
        return HttpEndPoint.transmute(ret.result);
    }
  1. 更改新函数的返回类型。这会导致编译错误,但它与新函数隔离。通过返回 Holder 类中放置的相同值来更正编译错误。
    public BusinessResult newMethod(ReturnHolder ret) {     // change 1. causes compile errors
        if (flag) {
            return BusinessResult.OK;           // change 2. fixes one compile error
        }
        else {
            return BusinessResult.FAIL;                     // change 3. fixes last error.
        }
    }
  1. 在旧函数中,使用新函数的新返回值作为映射函数的参数。
    public JavaxWsResponse doLogic() {
        ReturnHolder ret = new ReturnHolder();

        return HttpEndPoint.transmute(newMethod(ret));  // pass result of new method to transmute.
    }
  1. 从新方法签名中删除 Holder 参数。

  2. 删除 Holder 类。

  3. 对于旧方法的每次使用:内联旧方法,删除其使用。 如果旧函数被子类覆盖,则需要对子类执行相同的重构以更改为新签名。

// BFORE
    public JavaxWsResponse runBusinessLogic(String payload) {
        if (isValid (payload)) {
            return new BusinessLogic(payload).doLogic();
        }
        else {
            return JavaxWsResponse.build("error");
        }
    }
// AFTER
    public JavaxWsResponse runBusinessLogic(String payload) {
        if (isValid (payload)) {
            return HttpEndPoint.transmute(new BusinessLogic(payload).newMethod()); 
        }
        else {
            return JavaxWsResponse.build("error");
        }
    }
  1. 对于映射函数的每次使用,请考虑是否可以简化或使逻辑更清晰。

  2. 将新方法重命名为旧方法。除非你更喜欢这个新名字。

© www.soinside.com 2019 - 2024. All rights reserved.