標籤:技術 參數化 span isp object mic atom ble 調用
iOS設計模式 - 命令
原理圖
說明
命令對象封裝了如何對目標執行指令的資訊,因此用戶端或調用者不必瞭解目標的任何細節,卻仍可以對他執行任何已有的操作。通過把請求封裝成對象,用戶端可以把它參數化共置入隊列或日誌中,也能夠支援可撤銷操作。命令對象將一個或多個動作綁定到特定的接收器。命令模式消除了作為對象的動作和執行它的接收器之間的綁定。
//// Invoker.h// CommandPattern//// Created by YouXianMing on 15/10/17.// Copyright ? 2015年 YouXianMing. All rights reserved.//#import <Foundation/Foundation.h>#import "CommandProtocol.h"@interface Invoker : NSObject/** * 單例 * * @return 單例 */+ (instancetype)sharedInstance;/** * 添加並執行 * * @param command 命令 */- (void)addAndExecute:(id <CommandProtocol>)command;@end
//// Invoker.m// CommandPattern//// Created by YouXianMing on 15/10/17.// Copyright ? 2015年 YouXianMing. All rights reserved.//#import "Invoker.h"@interface Invoker ()@property (nonatomic, strong) NSMutableArray *commandQueue;@end@implementation Invoker+ (instancetype)sharedInstance { static Invoker *sharedInstanceValue = nil; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ sharedInstanceValue = [[Invoker alloc] init]; sharedInstanceValue.commandQueue = [NSMutableArray array]; }); return sharedInstanceValue;}- (void)addAndExecute:(id <CommandProtocol>)command { // 添加並執行 [self.commandQueue addObject:command]; [command execute];}@end
//// CommandProtocol.h// CommandPattern//// Created by YouXianMing on 15/10/17.// Copyright ? 2015年 YouXianMing. All rights reserved.//#import <Foundation/Foundation.h>@protocol CommandProtocol <NSObject>@required/** * 執行指令 */- (void)execute;@end
細節
iOS設計模式 - 命令