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 ...
随机推荐
- MySQL(Percona Server) 5.6 主从复制
MySQL(Percona Server) 5.6.15 主库:192.168.2.21 从库:192.168.2.22 例如我们同步的数据库为:test. 如果需要同步多个数据库下面会有说明. My ...
- 使用安卓手机上的shh软件ConnectBot管理您的Linux服务器
ConnectBot是一款在Android手机上通过命令行方式连接管理类Unix系统的软件(类Unix系统包含:FreeBSD.OpenBSD.NetBSD.Solaris.Mac.AIX.GUN/L ...
- MVC中使用EF的技巧集
1.从数据库生成模型后,再次更新模型时,之前设置的验证规则会丢失. 解决方法:在Models文件夹中新建一个空白类,把它命名为shujuyanzh.cs(类名可以自定),然后把Models中自动生成的 ...
- 被swoole坑哭的PHP程序员
被swoole坑哭的PHP程序员 2015-09-16 09:57 文帅营 博客园 字号:T | T 首先说一下对swoole的理解:披着PHP外衣的C程序.很多PHPer朋友看到swoole提供的强 ...
- Apache2 worker
http://www.php-internals.com/book/?p=chapt08/08-03-zend-thread-safe-in-php 在多线程系统中,进程保留着资源所有权的属性,而多个 ...
- 3D滚动下拉菜单-简直不要太任性
预览 先看看最终效果 简介 由来 最初看到这个是在14年5月,猛戳这里:妙味官网,觉得非常炫.想要做出来,所以就开始学习web. 那时候是做c/s的,也因为这个走上了b/s之路,(゚Д゚≡゚Д゚) 现 ...
- Vmware安装Centos NAT方式设置静态IP
[Vmware中在搭建集群环境等,DHCP自动获取IP方式不方便,为了固定IP减少频繁更改配置信息,建议使用静态IP来配置,网络连接主要有三种方式 1.nat 2.桥接,3主机模式 ,在这里主要介NA ...
- C# fun
C#中Func<T,TResult>的用法和Lambda表达式 代码片段: Func<int,string,string> t=(int x, string b) => ...
- How to use groovy script on jenkins
1. Install groovy plugin 2. Add a step of groovy. (normal & systerm) 3. Execute groovy script im ...
- web网站获取客户端mac地址
<HTML><HEAD><TITLE>WMI Scripting HTML</TITLE> <META http-equiv=Content-Ty ...