Martin Fowler在他的refactoring中描述了很多这样的例子,Kent Beck则在Smalltalk Best Practice Pattern中更基础地揭示了隐含在这些refactoing下的意图。 下面是一个实际的例子,来自于Martin Fowler在ACM上的设计专栏: class Invoice... String asciiStatement() {
StringBuffer result = new StringBuffer(); result.append(“Bill for “ + customer + “”); Iterator it = items.iterator(); while(it.hasNext()) { LineItem each = (LineItem) it.next(); result.append(“ ” + each.product() + “ ” + each.amount() + “”); } result.append(“total owed:” + total + “”); return result.toString(); } String HtmlStatement() { StringBuffer result = new StringBuffer(); result.append(“ Bill for ” + customer + “
”); result.append(“”); Iterator it = items.iterator(); while(it.hasNext()) { LineItem each = (LineItem) it.next(); result.append(“ ” + each.product() + “ ” + each.amount() + “ ”); } result.append(“ ”); result.append(“ total owed:” + total + “
参见:Martin Fowler:Refactoring:Improve the design of Existing Code Kent Beck : Smalltalk Best Pratice Pattern ACM: Martin Fowler Design column:Reduce repetation Kent Beck: Extreme Programming Explained