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