• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Swift语言之命令模式(CommandPattern)实现

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

今天遇到这样一个问题,我现在有一个整数数组,如:

var numbers = [3, 7, 12, 9, 200]

现需要对其中的每一个数字都执行一系列相同的加减乘除操作,如对每一个数字都加5乘8再减去1,但是这样的操作在编译时并不确定,需要在运行时由用户指定;

一看到这个题目,当然就想到了用设计模式中的命令模式来实现;

于是先写了这样的一个类:

class Calculator {
private(set)
var total = 0 required init(value: Int){ total = value } func add(amount: Int) { total += amount } func substract(amount: Int) { total -= amount } func multiply(amount: Int) { total = total * amount } func divide(amount: Int) { total = total / amount } }

这个类用于实现对某个数执行不同的操作。

下一步中我创建了一个Command类,用于记录需要执行的操作:

struct Command {
    
    typealias OperationType = (Calculator -> Int -> Void)
    
    let operation: OperationType
    let amount: Int
    
    init(operation: OperationType, amount: Int) {
        self.operation = operation
        self.amount = amount
    }
    
    func execute(calculator: Calculator) {
        operation(calculator)(amount)
    }
}

在该类中我定义了一个名为OperationType的类型别名,从定义中可以看出,OperationType类型的实例是一个Closure,该Closure接受一个Calculator类型的实例,并返回一个类型为Int -> Void的Closure(此Closure的类型就是Calculator中每个方法的类型);

amount为执行那个操作的参数值;

待一切完毕后,由用户来指定要执行的命令,如将数组numbers中的每一个值加5乘8再减去1,则可以这样实现:

var commands = [Command]()
commands.append(Command(operation: Calculator.add, amount: 5))
commands.append(Command(operation: Calculator.multiply, amount: 8))
commands.append(Command(operation: Calculator.substract, amount: 1))

这样做并没有达到Command设计模式想要达到的目的,及实现命令的发出者与命令的执行者之间的脱耦;于是我加入了一个新的enum类型:

enum OperationType {
    case Add(Int)
    case Substract(Int)
    case Multiply(Int)
    case Divide(Int)
}

这个enum类型中的每一个成员都自带一个关联值,及要执行操作的参数,于是Command类型可以改变为:

struct Command {
    
    let operationType: OperationType
    
    init(operationType: OperationType) {
        self.operationType = operationType
    }
    
    func execute(calculator: Calculator) {
        switch self.operationType {
            case .Add(let value):
                calculator.add(value)
            case .Substract(let value):
                calculator.substract(value)
            case .Multiply(let value):
                calculator.multiply(value)
            case .Divide(let value):
                calculator.divide(value)
        }
    }
}

重新创建Commands数组:

var commands = [Command]()
commands.append(Command(operationType: .Add(5)))
commands.append(Command(operationType: .Multiply(8)))
commands.append(Command(operationType: .Substract(1)))

OK,执行所有命令:

for number in numbers {
    var calculator = Calculator(value: number)
    
    for command in commands {
        command.execute(calculator)
    }
    
    println("\(number) -> \(calculator.total)")
}

这里是结果:


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
swift学习-17--析构器发布时间:2022-07-13
下一篇:
【转载】OpenStackSwift学习笔记发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap