版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/83104331
将大型方法放入单独对象,使方法内的临时变量成为对象中字段,在对象中将大型方法分解为多个小型方法
更多精彩
- 更多技术博客,请移步 asing1elife’s blog
前置条件
- 有时候会发现无法使用 6.4 Replace Temp with Query 以查询取代临时变量 ,因为无法拆解一个需要被拆解的对象
- 这个时候就需要先使用本方法,在使用上述方法
动机
- 小型方法的复用率更高,且更容易被理解
案例
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 方法
}