9. 關於 Performance 這件事… Performance 應該是在最佳化效能時再來擔心,在重構的階段先不用擔心這件事情 Performance 應該是透過工具來檢測,肉眼是很難看出來的 回想重構的目的… 讓程式更容易被讀取 讓程式更容易被擴充 The best way to optimize performance is to first write a well factored program, then optimize it
15. 重構Step7: Replace Type Code with State/Strategy 作法(1/2): Replace Type Code with State/Strategy: 將 type code behavior 搬移到 Strategy Pattern 讓金額計算的邏輯不再綁死在 Movie ,所以將金額計算的邏輯搬移到 Price 家族 Self Encapsulate Field 時機: 當想要存取一個 super class 的 field,卻在 sub class 改變它的 value 作法: 為 field 建立 setter 與 getter,只用這些 methods 來存取這個 field(即使在同一個 class 內) Move Method Example: chp1.movie_rental.v7.strategy_pattern
16. 重構Step8: Replace Conditional with Polymorphism 作法(2/2): Replace Conditional with Polymorphism: 在 sub Price classes 內建立 overriding method 來取代 super Price class 的 switch 將 super Price class 的 getCharge() 改為 abstract Example: chp1.movie_rental.v7.strategy_pattern 方便: 為了積點的預設值,所以不將 getFrequentRenterPoints() 也改成 abstract public class NewReleasePriceextends Price { double getCharge(intdaysRented) { return (double) daysRented * 3; } public abstract class Price{ double getCharge(intdaysRented) { double result = 0; switch (getPriceCode()) { case Movie.NEW_RELEASE: result += (double) daysRented * 3; break; } return result; }