Swift设计模式之命令模式

Swift设计模式之命令模式,第1张

概述转自 Swift设计模式 原文 Design-Patterns-In-Swift // 命令模式// 百度百科:一组行为抽象为对象,实现二者之间的松耦合// 设计模式分类:行为型模式/** * 命令接口 */protocol LightCommand { /** 执行命令 - returns: 结果 */ func execute() -> String}/// 打

转自

Swift设计模式

原文

Design-Patterns-In-Swift
// 命令模式// 百度百科:一组行为抽象为对象,实现二者之间的松耦合// 设计模式分类:行为型模式/** * 命令接口 */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()
总结

以上是内存溢出为你收集整理的Swift设计模式之命令模式全部内容,希望文章能够帮你解决Swift设计模式之命令模式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1076303.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存