原文鏈接: https://www.natashatherobot.com/refactoring-to-creation-method/
作者: Natasha The Robot
原文日期: 2016-05-27
譯者: Crystal Sun
我上個月參加了幾次會議并發(fā)表演講鹿霸,Refactoring to Patterns 這本書被多次提及几苍,特別是在我喜歡的研討會上,出現(xiàn)多次缀程。我最終還是沒太有時間讀(至少是在 WWDC 大會發(fā)布后的瘋狂情緒高漲之前)搜吧,我想先存檔我喜歡的模式以供將來參考。我還發(fā)現(xiàn)杨凑,通過把書中的 Java 代碼翻譯成 Swift滤奈,我能更好地記住這些知識點。
第一個模式就是創(chuàng)建方法撩满。
重構(gòu)之前
先假設你有一個 model蜒程,比如是借款,model 里有一堆不同的初始化構(gòu)造器(initializer)伺帘。在這個例子中昭躺,每個初始化構(gòu)造器(initializer)都有自己存在的原因,并且對應這不同的借款類型:
struct Loan {
let commitment: NSDecimalNumber
let riskRating: Float
let maturity: Int
let expiry: NSDate?
let capitalStrategy: String?
let outstanding: NSDecimalNumber?
init(commitment: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate?, capitalStrategy: String?, outstanding: NSDecimalNumber?) {
self.commitment = commitment
self.riskRating = riskRating
self.maturity = maturity
self.expiry = expiry
self.capitalStrategy = capitalStrategy
self.outstanding = outstanding
}
init(commitment: NSDecimalNumber, riskRating: Float, maturity: Int) {
self.init(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: nil, capitalStrategy: nil, outstanding: nil)
}
init(commitment: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate) {
self.init(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: expiry, capitalStrategy: nil, outstanding: nil)
}
init(commitment: NSDecimalNumber, outstanding: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate) {
self.init(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: expiry, capitalStrategy: nil, outstanding: outstanding)
}
init(capitalStrategy: String, commitment: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate) {
self.init(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: expiry, capitalStrategy: capitalStrategy, outstanding: nil)
}
init(capitalStrategy: String, commitment: NSDecimalNumber, outstanding: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate) {
self.init(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: expiry, capitalStrategy: capitalStrategy, outstanding: outstanding)
}
}
看看這代碼伪嫁,你會完全沒有思路(除非你知道所有的借貸類型)领炫,不知道該用哪個初始化構(gòu)造器(initializer)。當然了张咳,一些新人剛剛接觸這代碼的時候可能還對項目的業(yè)務不是很清楚帝洪,所以他們可能會用錯初始化構(gòu)造器(initializer),甚至會困惑這些初始化構(gòu)造器(initializer)的作用意圖脚猾。
是時候來重構(gòu)了葱峡!
重構(gòu)之后
用創(chuàng)建方法(Creator Method)重構(gòu)后的代碼如下:
struct Loan {
let commitment: NSDecimalNumber
let riskRating: Float
let maturity: Int
let expiry: NSDate?
let capitalStrategy: String?
let outstanding: NSDecimalNumber?
// 原來的初始化構(gòu)造器(initializer)現(xiàn)在現(xiàn)在可以是私有的
private init(commitment: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate?, capitalStrategy: String?, outstanding: NSDecimalNumber?) {
self.commitment = commitment
self.riskRating = riskRating
self.maturity = maturity
self.expiry = expiry
self.capitalStrategy = capitalStrategy
self.outstanding = outstanding
}
static func createTermLoan(commitment: NSDecimalNumber, riskRating: Float, maturity: Int) -> Loan {
return Loan(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: nil, capitalStrategy: nil, outstanding: nil)
}
static func createTermLoan(capitalStrategy: String, commitment: NSDecimalNumber, outstanding: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate) -> Loan {
return Loan(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: expiry, capitalStrategy: capitalStrategy, outstanding: outstanding)
}
static func createRevolverLoan(commitment: NSDecimalNumber, outstanding: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate) -> Loan {
return Loan(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: expiry, capitalStrategy: nil, outstanding: outstanding)
}
static func createRevolverLoan(capitalStrategy: String, commitment: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate) -> Loan {
return Loan(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: expiry, capitalStrategy: capitalStrategy, outstanding: nil)
}
static func createRCTL(commitment: NSDecimalNumber, riskRating: Float, maturity: Int, expiry: NSDate) -> Loan {
return Loan(commitment: commitment, riskRating: riskRating, maturity: maturity, expiry: expiry, capitalStrategy: nil, outstanding: nil)
}
}
注意到重構(gòu)后的代碼多么漂亮了嗎!當你初始化一個借貸時龙助,你知道它的作用了砰奕。
雖然我一般不會在類里使用這么多的初始化構(gòu)造器(initializer),根據(jù)不同的實際情況泌参,設置可能還有更好的方法(比如:枚舉)脆淹,但我一定會考慮重構(gòu)成這種模式,讓我的代碼更具可讀性沽一,更加簡潔盖溺。
本文由 SwiftGG 翻譯組翻譯,已經(jīng)獲得作者翻譯授權(quán)铣缠。