意图
- 每个函数的粒度都很小,被复用的机会就更大
- 使高层函数读起来就像一系列注释
- 如果函数是细粒度,覆写也会更容易
示例
/** * 提取方法之后 * Created by luo on 2017/3/4. */ public class ExtractMethodBefore { private String _name; void printOwing(double amount) { printBanner(); // print details System.out.println("name:"+_name); System.out.println("amount:"+amount); } private void printBanner() { } } /** * 提取方法之后 * Created by luo on 2017/3/4. */ public class ExtractMethodAfter { private String _name; void printOwing(double amount){ printBanner(); printDetails(amount); } private void printBanner() { } public void printDetails(double amount) { System.out.println("name:"+_name); System.out.println("amount:"+amount); } }