1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
* 命令接口 */ protocol LightCommand { 执行命令 - returns: 结果 */ func execute() -> String }
class OpenCommand : LightCommand { let light:String required init(light: String) { self.light = light } func execute() -> String { return "Opened \(light)" } }
class CloseCommand : LightCommand { let light:String required init(light: String) { self.light = light } func execute() -> String { return "Closed \(light)" } }
class TableLamp { let openCommand: LightCommand let closeCommand: LightCommand init(light: String) { self.openCommand = OpenCommand(light:light) self.closeCommand = CloseCommand(light:light) } func close() -> String { return closeCommand.execute() } func open() -> String { return openCommand.execute() } }
let lightName = "名叫[hello world!]的台灯" let myTableLamp = TableLamp(light:lightName)
myTableLamp.open() myTableLamp.close()
|