swift - The command pattern
2015-09-01 23:41
531 查看
The command pattern provides a mechanism by which details of how to invoke a method can beencapsulated so that the method can be invoked later or by a different component.
命令模式:把每一次执行的命令放到一个队列里,undo队列 和 do队列,实现了undo/redo的操作。
client:
let calc = Calculator();
calc.add(10);
calc.multiply(4);
calc.subtract(2);
println("Calc 1 Total: \(calc.total)");
let macro = calc.getMacroCommand();
let calc2 = Calculator();
macro(calc2);
println("Calc 2 Total: \(calc2.total)");
///////////////////////////////////////////////////////////////////////////////////////////
pattern:
//1
import Foundation;
class Calculator {
private(set) var total =
0;
typealias CommandClosure = (Calculator ->
Void);
private var history = [CommandClosure]();
private var queue = dispatch_queue_create("arrayQ", DISPATCH_QUEUE_SERIAL);
func add(amount:Int) {
addMacro(Calculator.add, amount: amount);
total += amount;
}
func subtract(amount:Int) {
addMacro(Calculator.subtract, amount: amount);
total -= amount;
}
func multiply(amount:Int) {
addMacro(Calculator.multiply, amount: amount);
total = total * amount;
}
func divide(amount:Int) {
addMacro(Calculator.divide, amount: amount);
total = total / amount;
}
private func addMacro(method:Calculator ->
Int -> Void, amount:Int) {
dispatch_sync(self.queue, {()
in
self.history.append({ calc
in method(calc)(amount)});
});
}
func getMacroCommand() -> (Calculator ->
Void) {
var commands = [CommandClosure]();
dispatch_sync(queue, {()
in
commands =
self.history
});
return { calc in
if (commands.count >
0) {
for index in
0 ..< commands.count {
commands[index](calc);
}
}
};
}
}
//2
protocol Command {
func execute(receiver:Any);
}
class CommandWrapper :
Command {
private let commands:[Command];
init(commands:[Command]) {
self.commands = commands;
}
func execute(receiver:Any) {
for command in
commands {
command.execute(receiver);
}
}
}
class GenericCommand<T> :
Command {
private var instructions:
T -> Void;
init(instructions:
T -> Void) {
self.instructions = instructions;
}
func execute(receiver:Any) {
if let safeReceiver = receiver
as? T {
instructions(safeReceiver);
}
else {
fatalError("Receiver is not expected type");
}
}
class func createCommand(instuctions:
T -> Void) ->
Command {
return GenericCommand(instructions: instuctions);
}
}
命令模式:把每一次执行的命令放到一个队列里,undo队列 和 do队列,实现了undo/redo的操作。
client:
let calc = Calculator();
calc.add(10);
calc.multiply(4);
calc.subtract(2);
println("Calc 1 Total: \(calc.total)");
let macro = calc.getMacroCommand();
let calc2 = Calculator();
macro(calc2);
println("Calc 2 Total: \(calc2.total)");
///////////////////////////////////////////////////////////////////////////////////////////
pattern:
//1
import Foundation;
class Calculator {
private(set) var total =
0;
typealias CommandClosure = (Calculator ->
Void);
private var history = [CommandClosure]();
private var queue = dispatch_queue_create("arrayQ", DISPATCH_QUEUE_SERIAL);
func add(amount:Int) {
addMacro(Calculator.add, amount: amount);
total += amount;
}
func subtract(amount:Int) {
addMacro(Calculator.subtract, amount: amount);
total -= amount;
}
func multiply(amount:Int) {
addMacro(Calculator.multiply, amount: amount);
total = total * amount;
}
func divide(amount:Int) {
addMacro(Calculator.divide, amount: amount);
total = total / amount;
}
private func addMacro(method:Calculator ->
Int -> Void, amount:Int) {
dispatch_sync(self.queue, {()
in
self.history.append({ calc
in method(calc)(amount)});
});
}
func getMacroCommand() -> (Calculator ->
Void) {
var commands = [CommandClosure]();
dispatch_sync(queue, {()
in
commands =
self.history
});
return { calc in
if (commands.count >
0) {
for index in
0 ..< commands.count {
commands[index](calc);
}
}
};
}
}
//2
protocol Command {
func execute(receiver:Any);
}
class CommandWrapper :
Command {
private let commands:[Command];
init(commands:[Command]) {
self.commands = commands;
}
func execute(receiver:Any) {
for command in
commands {
command.execute(receiver);
}
}
}
class GenericCommand<T> :
Command {
private var instructions:
T -> Void;
init(instructions:
T -> Void) {
self.instructions = instructions;
}
func execute(receiver:Any) {
if let safeReceiver = receiver
as? T {
instructions(safeReceiver);
}
else {
fatalError("Receiver is not expected type");
}
}
class func createCommand(instuctions:
T -> Void) ->
Command {
return GenericCommand(instructions: instuctions);
}
}
相关文章推荐
- Swift1
- 初步swift语言学习笔记2(可选类型?和隐式可选类型!)
- Swift Tutorial(swift 教程) 4:Your First App
- swift开发
- swift 总结3
- 女神画廊App (Swift1.2)
- swift总结2
- 最老程序员创业开发实训5---Swift语言极速入门
- 相亲神器记录Swift1.2
- swift总结1
- swift自学注意点
- swift 中#pragma mark 表示方法
- swift详解之二十--------在xcode中使用git管理源代码,将代码提交到github
- swift 添加按钮
- swift - The Chain Responsibility pattern
- swift详解之十九--------------UITableView的基本操作(下拉刷新,新增删除,分组,检索等)
- IOS--Swift 01
- Swift UILabel 文字大小随着宽度调整
- Swift UILabel常用功能
- Swift子类初始化函数中调用父类初始化函数的正确位置