委托是一種設(shè)計(jì)模式,它允許類或結(jié)構(gòu)體將一些需要它們負(fù)責(zé)的功能交由(委托)給其他的類型。
委托模式的實(shí)現(xiàn)很簡(jiǎn)單: 定義協(xié)議來封裝那些需要被委托的函數(shù)和方法, 使其遵循者擁有這些被委托的函數(shù)和方法。
委托模式可以用來響應(yīng)特定的動(dòng)作或接收外部數(shù)據(jù)源提供的數(shù)據(jù),而無需要知道外部數(shù)據(jù)源的類型。
下文是兩個(gè)基于骰子游戲的協(xié)議:
protocol DiceGame {
var dice: Dice { get }
func play()
}
protocol DiceGameDelegate {
func gameDidStart(game: DiceGame)
func game(game: DiceGame, didStartNewTurnWithDiceRoll diceRoll:Int)
func gameDidEnd(game: DiceGame)
}
DiceGame協(xié)議可以在任意含有骰子的游戲中實(shí)現(xiàn),DiceGameDelegate協(xié)議可以用來追蹤DiceGame的游戲過程。
如下所示,SnakesAndLadders是Snakes and Ladders(譯者注:控制流章節(jié)有該游戲的詳細(xì)介紹)游戲的新版本。新版本使用Dice作為骰子,并且實(shí)現(xiàn)了DiceGame和DiceGameDelegate協(xié)議
class SnakesAndLadders: DiceGame {
let finalSquare = 25
let dic = Dice(sides: 6, generator: LinearCongruentialGenerator())
var square = 0
var board: Int[]
init() {
board = Int[](count: finalSquare + 1, repeatedValue: 0)
board[03] = +08; board[06] = +11; borad[09] = +09; board[10] = +02
borad[14] = -10; board[19] = -11; borad[22] = -02; board[24] = -08
}
var delegate: DiceGameDelegate?
func play() {
square = 0
delegate?.gameDidStart(self)
gameLoop: while square != finalSquare {
let diceRoll = dice.roll()
delegate?.game(self,didStartNewTurnWithDiceRoll: diceRoll)
switch square + diceRoll {
case finalSquare:
break gameLoop
case let newSquare where newSquare > finalSquare:
continue gameLoop
default:
square += diceRoll
square += board[square]
}
}
delegate?.gameDIdEnd(self)
}
}
游戲的初始化設(shè)置(setup)被SnakesAndLadders類的構(gòu)造器(initializer)實(shí)現(xiàn)。所有的游戲邏輯被轉(zhuǎn)移到了play方法中。
注意: 因?yàn)?code style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; -webkit-font-smoothing: antialiased; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 14px; padding: 0px 5px; color: rgb(199, 37, 78); background-color: rgb(248, 248, 248); white-space: nowrap; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; margin: 0px 2px; border: 1px solid rgb(234, 234, 234);">delegate并不是該游戲的必備條件,delegate被定義為遵循DiceGameDelegate協(xié)議的可選屬性。
DicegameDelegate協(xié)議提供了三個(gè)方法用來追蹤游戲過程。被放置于游戲的邏輯中,即play()方法內(nèi)。分別在游戲開始時(shí),新一輪開始時(shí),游戲結(jié)束時(shí)被調(diào)用。
因?yàn)?code style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; -webkit-font-smoothing: antialiased; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 14px; padding: 0px 5px; color: rgb(199, 37, 78); background-color: rgb(248, 248, 248); white-space: nowrap; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; margin: 0px 2px; border: 1px solid rgb(234, 234, 234);">delegate是一個(gè)遵循DiceGameDelegate的可選屬性,因此在play()方法中使用了可選鏈來調(diào)用委托方法。 若delegate屬性為nil, 則委托調(diào)用優(yōu)雅地失效。若delegate不為nil,則委托方法被調(diào)用
如下所示,DiceGameTracker遵循了DiceGameDelegate協(xié)議
class DiceGameTracker: DiceGameDelegate {
var numberOfTurns = 0
func gameDidStart(game: DiceGame) {
numberOfTurns = 0
if game is SnakesAndLadders {
println("Started a new game of Snakes and Ladders")
}
println("The game is using a \(game.dice.sides)-sided dice")
}
func game(game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int) {
++numberOfTurns
println("Rolled a \(diceRoll)")
}
func gameDidEnd(game: DiceGame) {
println("The game lasted for \(numberOfTurns) turns")
}
}
DiceGameTracker實(shí)現(xiàn)了DiceGameDelegate協(xié)議的方法要求,用來記錄游戲已經(jīng)進(jìn)行的輪數(shù)。 當(dāng)游戲開始時(shí),numberOfTurns屬性被賦值為0;在每新一輪中遞加;游戲結(jié)束后,輸出打印游戲的總輪數(shù)。
gameDidStart方法從game參數(shù)獲取游戲信息并輸出。game在方法中被當(dāng)做DiceGame類型而不是SnakeAndLadders類型,所以方法中只能訪問DiceGame協(xié)議中的成員。
上一篇:Swift引入聲明下一篇:Swift集合的可變性