iOS設計模式---命令模式(未完)

來源:互聯網
上載者:User

標籤:

命令模式:將請求封裝為一個對象,從而可用不同的請求對客戶進行參數化,對請求排隊或記錄請求日誌,以及支援可撤銷的操作。

Command: 為Invoker所知的通用介面(協議)

ConcreteCommand: 具體的命令對象,將Receiver(執行者)與action(實際操作)進行綁定

Receiver: 執行實際操作的對象

Invoker: 命令調用者,接收通用命令


Objective-C 樣本:

Command:

////  NimoCommand.h//  CommandDemo////  Created by Tony on 15/8/13.//  Copyright (c) 2015年 NimoWorks. All rights reserved.//#import <Foundation/Foundation.h>@protocol NimoCommand <NSObject>- (void)execute;@end

ConcreteCommand:

////  NimoConcreteCommand.h//  CommandDemo////  Created by Tony on 15/8/13.//  Copyright (c) 2015年 NimoWorks. All rights reserved.//#import <Foundation/Foundation.h>#import "NimoCommand.h"@class NimoReceiver;@interface NimoConcreteCommand : NSObject <NimoCommand>@property (nonatomic) NimoReceiver *receiver;- (id)initWithReceiver:(NimoReceiver *)receiver;@end
////  NimoConcreteCommand.m//  CommandDemo////  Created by Tony on 15/8/13.//  Copyright (c) 2015年 NimoWorks. All rights reserved.//#import "NimoConcreteCommand.h"#import "NimoReceiver.h"@implementation NimoConcreteCommand- (void)execute{    [_receiver action];}- (id)initWithReceiver:(NimoReceiver *)receiver{    if (self = [super init]) {        _receiver = receiver;    }        return self;}@end

Receiver:

////  NimoReceiver.h//  CommandDemo////  Created by Tony on 15/8/13.//  Copyright (c) 2015年 NimoWorks. All rights reserved.//#import <Foundation/Foundation.h>@interface NimoReceiver : NSObject- (void)action;@end
////  NimoReceiver.m//  CommandDemo////  Created by Tony on 15/8/13.//  Copyright (c) 2015年 NimoWorks. All rights reserved.//#import "NimoReceiver.h"@implementation NimoReceiver- (void)action{    NSLog(@"實際執行");}@end

Invoker:

////  NimoInvoker.h//  CommandDemo////  Created by Tony on 15/8/13.//  Copyright (c) 2015年 NimoWorks. All rights reserved.//#import <Foundation/Foundation.h>#import "NimoCommand.h"@interface NimoInvoker : NSObject@property (nonatomic, weak) id<NimoCommand> command;- (void)executeCommand;@end
////  NimoInvoker.m//  CommandDemo////  Created by Tony on 15/8/13.//  Copyright (c) 2015年 NimoWorks. All rights reserved.//#import "NimoInvoker.h"@implementation NimoInvoker- (void)executeCommand{    [_command execute];}@end

Client:

////  main.m//  CommandDemo////  Created by Tony on 15/8/13.//  Copyright (c) 2015年 NimoWorks. All rights reserved.//#import <Foundation/Foundation.h>#import "NimoReceiver.h"#import "NimoInvoker.h"#import "NimoConcreteCommand.h"int main(int argc, const char * argv[]) {    @autoreleasepool {               NimoReceiver *receiver = [[NimoReceiver alloc] init];        NimoConcreteCommand *command = [[NimoConcreteCommand alloc] initWithReceiver:receiver];                NimoInvoker *invoker = [[NimoInvoker alloc] init];        invoker.command = command;        [invoker executeCommand];            }    return 0;}

Running:

2015-08-13 22:49:56.412 CommandDemo[1385:43303] 實際執行


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.