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)")
}
这里是结果:

Swift语言之命令模式(Command Pattern)实现的更多相关文章
- 设计模式 - 命令模式(command pattern) 多命令 具体解释
命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...
- 设计模式 - 命令模式(command pattern) 具体解释
命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...
- 设计模式 - 命令模式(command pattern) 宏命令(macro command) 具体解释
命令模式(command pattern) 宏命令(macro command) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考: 命名模式(撤销) ...
- 乐在其中设计模式(C#) - 命令模式(Command Pattern)
原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...
- 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释
命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...
- 二十四种设计模式:命令模式(Command Pattern)
命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...
- 设计模式-15命令模式(Command Pattern)
1.模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使 ...
- 设计模式(六):控制台中的“命令模式”(Command Pattern)
今天的博客中就来系统的整理一下“命令模式”.说到命令模式,我就想起了控制台(Console)中的命令.无论是Windows操作系统(cmd.exe)还是Linux操作系统(命令行式shell(Comm ...
- 十一个行为模式之命令模式(Command Pattern)
定义: 将一个请求封装成对象,使得请求发送者和请求接受者之间相互隔离,消除两者之间的耦合.引入命令类,使得不同请求对客户参数化,并且可以对命令添加附件操作,如:排队.撤销.日志.组合等. 结构图: C ...
随机推荐
- javascript平时例子⑧(大屏轮播)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Django 基本命令
1. 新建一个 django project django-admin.py startproject project-name 一个 project 为一个项目,project-name 项目名称, ...
- GIT 操作
1. 查看某个文件某次提交修改的内容 git show commitid a.txt 2. git rm 和 git rm --cached 当我们需要删除暂存区或分支上的文件, 同时工作区也不 ...
- linux 源码安装详解
./configure是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不是需要CC或GCC,它是个shell脚本.make是用来编译的,它从Makefile中读取指令,然后编 ...
- 百度编辑器ueditor 的 submit 表单提交
页面中表单提交代码: <input type="submit" name="Submit" value="修改保存"> 提交的结 ...
- css 文字溢出隐藏 带省略号
.demo{ width:100px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } 注意宽度要设置
- Python 打包工具cx_freeze 问题记录及解决办法
在节前的最后一天,解决了打包过程中遇到的所有问题,可以成功运行了!真是个好彩头,希望新的一年一切顺利! 以下是在使用cx_freeze过程中遇到的问题及解决办法(Win7) 问题描述:运行exe,启动 ...
- C++求一个十进制的二进制中1的个数
int oneNumInBinary(int n){ ; while(n){ n = n&(n-); cnt++; } return cnt; }
- C#代码
http://www.cnblogs.com/zjfree/category/269738.html 超简易静态Web服务器 C# 生成不重复随机字符串 (1秒内生成1000000个) C# 读写IN ...
- css补充、JavaScript、Dom
css补充: position: fixed:可以将标签固定在页面的某个位置 absolute+relative:通过两者的结合可以让标签在一个相对的位置 代码例子:(通过fixed标签将某些内容固定 ...