6.8 Replace Method with Method Object 以方法对象取代方法

版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/83104331

将大型方法放入单独对象,使方法内的临时变量成为对象中字段,在对象中将大型方法分解为多个小型方法

更多精彩

前置条件

  1. 有时候会发现无法使用 6.4 Replace Temp with Query 以查询取代临时变量 ,因为无法拆解一个需要被拆解的对象
  2. 这个时候就需要先使用本方法,在使用上述方法

动机

  1. 小型方法的复用率更高,且更容易被理解

案例

public class Order {
	public double price() {
		double primaryPrice;
		double secondaryPrice;
		double tertiaryPrice;
	}
}
public class Order {
	public double price() {
		Price price = new Price();
	}
}

public class Price {
	private double primaryPrice;
	private double secondaryPrice;
	private double tertiaryPrice;

	// 提供一系列 getter/setter 方法
}

猜你喜欢

转载自blog.csdn.net/asing1elife/article/details/83104331