1. Extract Method(提煉函式)
將代碼段放進一個獨立函式中,并讓函式名稱解釋該函式的用途,
示例:
void printOwing(double amount) {
printBanner();
//print details
System.out.println("name: " + _name);
System.out.println("amount: " + amount);
}
重構為:
void printOwing(double amount) {
printBanner();
printDetails();
}
void printDetails(double amount) {
System.out.println("name: " + _name);
System.out.println("amount: " + amount);
}
2. Inline Method(行內函式)
在函式呼叫點插入函式本體,然后移除該函式,
示例:
int getRating() {
return moreThanFiveLateDeliveries() ? 2 : 1;
}
boolean moreThanFiveLateDeliveries() {
return _numberOfLateDeliveries > 5;
}
重構為:
int getRating() {
return (_numberOfLateDeliveries > 5) ? 2 : 1;
}
3. Inline Temp(行內臨時變數)
將所有對該變數的參考動作,替換為對它復制的那個運算式本身,
示例:
double basePrice = anOrder.BasePrice();
return (basePrice > 1000);
重構為:
return (anOrder.BasePrice() > 1000);
4. Replace Temp with Query(以查詢取代臨時變數)
將運算式提煉到一個獨立函式中,將這個臨時變數的所有參考點替換為對新函式的呼叫,此后,新函式就可被其他函式使用,
示例:
double basePrice = _quantity * _itemPrice;
if (basePrice > 1000) {
return basePrice * 0.95;
} else {
return basePrice * 0.98;
}
重構為:
if (basePrice() > 1000) {
return basePrice() * 0.95;
} else {
return basePrice() * 0.98;
}
...
double basePrice() {
return _quantity * _itemPrice;
}
5. Introduce Explaining Variable(引入解釋性變數)
將該復雜運算式(或其中一部分)的結果放進一個臨時變數,一次變數名稱來解釋運算式用途,
示例:
if ((platform.toUpperCase().indexOf("MAC") > -1) && (browser.toUpperCase().indexOf("IE") > -1) && wasInitialized() && resize > 0) {
//do something
}
重構為:
final boolean isMacOs = platform.toUpperCase().indexOf("MAC") > -1;
final boolean isIEBrowser = browser.toUpperCase().indexOf("IE") > -1;
final boolean wasResized = resize > 0;
if (isMacOs && isIEBrowser && wasInitialized() && wasResized) {
//do someting
}
6. Split Temporary Variable(分解臨時變數)
針對每次賦值,創造一個獨立,對應的臨時變數
示例:
double temp = 2 * (_height + _width);
System.out.println(temp);
temp = _height * _width;
System.out.println(temp);
重構為:
final double perimeter = 2 * (_height + _width);
System.out.println(perimeter);
final double area = _height * _width;
System.out.println(area);
7. Remove Assignments to Parameters(移除對引數的賦值)
以一個臨時變數取代該引數的位置
示例:
int discount (int inputVal, int quantity, int yearToDate) {
if (inputVal > 50) {
inputVal -= 2;
}
...
重構為:
int discount (int inputVal, int quantity, int yearToDate) {
int result = inputVal
if (inputVal > 50) {
result -= 2;
}
8. Replace Method with Method Object(以函式物件取代函式)
將這個函式放進一個單獨物件中,如此以來區域變數就成了對物件內的欄位,然后你可以在同一個物件中將這個大型函式分解為多個小型函式,
示例:
class Order...
double price() {
double primaryBasePrice;
double secondaryBasePrice;
double tertiaryBasePrice;
//long computation;
...
}
重構為:
class PriceCalculator {
private double primaryBasePrice;
private double secondaryBasePrice;
private double tertiaryBasePrice;
void compute() {
//long computation
}
}
class Order...
private PriceCalculator _priceCalculator = new PriceCalculator();
double price() {
return new PriceCalculator().compute();
...
9. Substitute Algorithm(替換演算法)
將函式邏輯本體替換為另一個簡潔清晰的演算法
示例:
String findPerson(String[] people) {
for (String person:people) {
if(person.equals("Don") || person.equals("John") || person.equals("Kent")) {
return person;
}
...
重構為:
String findPerson(String[] people) {
List candidates = Arrays.asList(new String[] {"Don", "John", "Kent"});
for(String person:people) {
if(candidates.contains(person)) {
return person;
}
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/44304.html
標籤:架構設計
上一篇:大型網站多級快取的分層架構
