Swift设计模式之命令模式

前端之家收集整理的这篇文章主要介绍了Swift设计模式之命令模式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

转自@H_301_5@

原文@H_301_5@

// 命令模式
// 百度百科:一组行为抽象为对象,实现二者之间的松耦合
// 设计模式分类:行为型模式

/** * 命令接口 */
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()
原文链接:https://www.f2er.com/swift/323810.html

猜你在找的Swift相关文章