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

    这里是结果:

  • 相关阅读:
    Linux的公平调度(CFS)原理
    linux内核分析——CFS(完全公平调度算法)
    Linux内核——进程管理之CFS调度器(基于版本4.x)
    QT中的qDebug()
    fork()函数(转)
    Linux下的进程1——进程概念,进程切换,上下文切换,虚拟地址空间
    Web Services 平台元素
    为什么使用 Web Services?
    WebServices简介
    CSS3(14)弹性盒子
  • 原文地址:https://www.cnblogs.com/cdutedu/p/4339685.html
Copyright © 2020-2023  润新知