• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hello World
This is the first demo for each programming language
            管理     

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

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

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)")
}

这里是结果:

posted @ 2015-03-15 13:57  Justin X  阅读(1959)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3