1.Decompose Conditional (分解條件運算式)
應用場景:你有一個復雜的條件(if-then-else)陳述句,從if、then、else三個段落中分別提煉出獨立函式,
示例:
if (date.before(SUMMER_START) || date.after(SUMMER_END)) {
charge = quantity * mWinterRate + mWinterServiceCharge;
} else {
charge = quantity * mSummerRate;
}
重構為:
if (notSummer(date)) {
charge = winterCharge(quantity);
} else {
charge = summerCharge(quantity);
}
private boolean notSummer(Date date) {
return date.before(SUMMER_START) || date.after(SUMMER_END);
}
private double winterCharge(int quantity) {
return quantity * mWinterRate + mWinterServiceCharge;
}
private double summerCharge(int quantity) {
return quantity * mSummerRate;
}
2.Consolidate Conditional Expression (合并條件運算式)
應用場景:你有一系列條件測驗,都得到相同結果,將這些測驗合并為一個條件運算式,并將這個條件運算式提煉成一個獨立函式,
示例:
double disabilityAmount() {
if(mSeniority < 2) {
return 0;
}
if(mMonthsDisabled > 12) {
return 0;
}
if(mIsPartTime) {
return 0;
}
// compute the disability amount ...
}
重構為:
double disabilityAmount() {
if(isNotEligibleForDisability()) {
return 0;
}
// compute the disability amount ...
}
private boolean isNotEligibleForDisability() {
return (mSeniority < 2) || (mMonthsDisabled > 12) || mIsPartTime;
}
3.Consolodate Duplicate Conditional Fragments (合并重復的條件片段)
應用場景:在條件運算式的每個分支上有著相同的一段代碼,將這段重復代碼搬移到條件運算式之外,
示例:
if(isSpecialDeal()) {
total = price * 0.95; send();
} else {
total = price * 0.98; send();
}
重構為:
if(isSpecialDeal()) {
total = price * 0.95;
} else {
total = price * 0.98;
}
send();
4.Remove Control Flag (移除控制標記)
應用場景:在一系列布爾運算式中,某個變數帶有“控制標記”的作用,以break陳述句或return陳述句取代控制標記,
示例:void checkSecurity(String[] people) {
boolean found = false;
for(int i = 0; i < people.length; i++) {
if(!found) {
if(people[i].equals("Don")) {
sendAlert();
found = true;
}
if(people[i].equals("John")) {
sendAlert();
found = true;
} } } }
重構為:void checkSecurity(String[] people) {
for(int i = 0; i < people.length; i++) {
if(people[i].equals("Don")) {
sendAlert();
break;
}
if(people[i].equals("John")) {
sendAlert();
break;
} } }
5.Replace Nested Conditional with Guard Clauses (以衛陳述句取代嵌套條件運算式)
應用場景:函式中的條件邏輯使人難以看清正常的執行路徑,使用衛陳述句表現所有特殊情況,
條件運算式常有兩種表現形式:a)所有分支都屬于正常行為;b)表達分支中只有一種是正常行為,其他都是不常見的情況,如果兩條分支都是正常行為,就應該使用形如if...else...的條件運算式;如果某個條件極其罕見,就應該單獨檢查該條件,并在該條件為真時立刻從函式中回傳,這樣的單獨檢查常被稱為“衛陳述句”,
示例:
double getPayAmount() {
double result; if(mIsDead) {
result = deadAmount();
} else {
if(mIsSeparated) {
result = separatedAmount();
} else {
if(mIsRetired) {
result = retiredAmount();
} else {
result = normalPayAmount();
} } }
return result;
}
重構為:
double getPayAmount() {
if(mIsDead) {
return deadAmount();
}
if(mIsSeparated) {
return separatedAmount();
}
if(mIsRetired) {
return retiredAmount();
}
return normalPayAmount();
}
6.Replace Conditional with Polymorphism (以多型取代條件運算式)
應用場景:你手上有個條件運算式,它根據物件型別的不同而選擇不同的行為,將這個條件運算式的每個分支放進一個子類內的覆寫函式中,然后將原始函式宣告為抽象函式,
示例:
double getSpeed() {
switch(mType) {
case EUROPEAN: return getBaseSpeed();
case AFRICAN: return getBaseSpeed() - getLoadFactor() * mNumberOfCoconuts;
case NORWEGIAN_BLUE: return mIsNailed ? 0 : getBaseSpeed(mVoltage);
}
throw new RuntimeException("Should be unreachable.");
}
重構為:
abstract class Bird {
abstract double getSpeed();
}
class European extends Bird {
double getSpeed() {
return getBaseSpeed();
} }
class African extends Bird() {
double getSpeed() {
return getBaseSpeed() - getLoadFactor() * mNumberOfCoconuts;
} }
class NorwegianBlue extends Bird {
double getSpeed() {
return mIsNailed ? 0 : getBaseSpeed(mVoltage);
} }
7. Introduce Null Object (引入Null物件)
應用場景:你需要再三檢查某物件是否為null,將null值替換為null物件,
示例:
if (custom == null) {
plan = BillingPlan.basic();
} else {
plan = custom.getPlan();
}
重構為:
class Custom {
public Plan getPlan() {
return normalPlan;
}
public boolean isNull() {
return false;
}
}
class NullCustom extends Custom {
public Plan getPlan() {
return BillingPlan.basic();
}
public boolean isNull() {
return true;
}
}
8. Introduce Assertion (引入斷言)
應用場景:某一段代碼需要對程式狀態做出某種假設,以斷言明確表現這種假設,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/32383.html
標籤:架構設計
下一篇:分布式事物之綜合案例分析
