iOS設計模式 - 命令

來源:互聯網
上載者:User

標籤:技術   參數化   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設計模式 - 命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.