重构:用方法对象替换方法说明

Pau*_*Pau 4 c# java refactoring coding-style

我正在寻找重构一个太长的方法。搜索我发现了这个技术: 用方法对象替换方法,但我根本不明白。

如果重构的方法是:

public class Order {
  //Method to refactor
  public double price() {
    double primaryBasePrice;
    double secondaryBasePrice;
    double tertiaryBasePrice;

     //compute impl
  }

 //.....
}
Run Code Online (Sandbox Code Playgroud)

在 Web 示例中,使用方法对象重构替换方法 将如下所示:

public class Order {

  //Method refactored
  public double price() {
    return new PriceCalculator(this).compute();
  }
  //.......
}

//Method object
public class PriceCalculator {
  private double primaryBasePrice;
  private double secondaryBasePrice;
  private double tertiaryBasePrice;

  public PriceCalculator(Order order) {
    //??
  }

  public double compute() {
    // impl
  }
}
Run Code Online (Sandbox Code Playgroud)

但是如何PriceCalculator获取primaryBasePrice, secondaryBasePrice,tertiaryBasePrice值进行计算?

我只看到可以在构造函数中传递值,如下所示:

//Method object
public class PriceCalculator {
  private double primaryBasePrice;
  private double secondaryBasePrice;
  private double tertiaryBasePrice;

  public PriceCalculator(Order order, double primaryBasePrice, 
     double secondaryBasePrice, double tertiaryBasePrice) {
    this.primaryBasePrice = primaryBasePrice;
    this.secondaryBasePrice = secondaryBasePrice;
    this.tertiaryBasePrice = tertiaryBasePrice;
  }

  public double compute() {
    // impl
  }
}
Run Code Online (Sandbox Code Playgroud)

否则,为什么要传入构造函数order实例引用?为什么需要?

  • 传递实例order

    return new PriceCalculator(this, primaryBasePrice, secondaryBasePrice, tertiaryBasePrice).compute();

  • 没有order实例参考:

    return new PriceCalculator(primaryBasePrice, secondaryBasePrice, tertiaryBasePrice).compute();

azu*_*rog 5

但是PriceCalculator 如何获取primaryBasePrice、secondaryBasePrice、thirdBasePrice 的值来进行计算呢?

与重构之前的方式相同。

如果你看一下原代码,primaryBasePricesecondaryBasePrice并且tertiaryBasePrice局部变量,其值正被在某处集合//compute impl部分。

  public double price() {
    double primaryBasePrice;
    double secondaryBasePrice;
    double tertiaryBasePrice;

     // compute impl
     // all base price variables are assigned somewhere in here
  }
Run Code Online (Sandbox Code Playgroud)

重构后,该compute()方法具有//compute impl代码的副本,并且只需将与PriceCalculator重构前的局部变量完全相同的值分配给其中的字段。

我们需要传入对Order对象的引用,以防这些值依赖于其方法或内部状态。

例如,如果我们之前有

public double price() {
    double primaryBasePrice;
    double secondaryBasePrice;
    double tertiaryBasePrice;

    // some of compute impl
    primaryBasePrice = getPrimary();
    secondaryBasePrice = getSecondary();
    tertiaryBasePrice = getTertiary();
    // the rest of compute impl
}
Run Code Online (Sandbox Code Playgroud)

重构后,我们会得到类似的东西

public double compute() {
    double primaryBasePrice;
    double secondaryBasePrice;
    double tertiaryBasePrice;

    // some of compute impl
    primaryBasePrice = order.getPrimary();
    secondaryBasePrice = order.getSecondary();
    tertiaryBasePrice = order.getTertiary();
    // the rest of compute impl
}
Run Code Online (Sandbox Code Playgroud)