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

夙愿已清 提交于 2019-11-26 15:44:16

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

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

这里是结果:

转载于:https://www.cnblogs.com/cdutedu/p/4339685.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!