iOS design Mode---command mode (not finished)
Last Update:2015-08-14
Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Command Mode:</strong> <i>encapsulates a request as an object that allows the client to be parameterized with different requests, to queue requests or log requests, and to support revocable Operations. </i><br></p></p> <p><p>Command: Common interface (protocol) known to Invoker</p></p> <p><p>Concretecommand: a specific command object that binds receiver (performer) to action (actual Operation)</p></p> <p><p>Receiver: the object performing the actual operation</p></p> <p><p>Invoker: command caller, receive generic command</p></p> <p><p><br></p></p> <p><p><strong>Objective-c example:</strong></p></p> <p><p>Command:</p></p> <pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">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</pre></pre> <p><p>Concretecommand:</p></p> <pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar:true; auto-links:false;">//// nimoconcretecommand.h// commanddemo//// created by tony on 15/8/13.// copyright (c) &NBSP;2015 year 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 </pre></pre> <pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">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</pre></pre> <p><p>Receiver:</p></p> <pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">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</pre></pre> <pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">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 (@ "actual execution");} @end</pre></pre> <p><p>Invoker:</p></p> <pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">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</pre></pre> <pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">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</pre></pre> <p><p>Client:</p></p> <pre class="brush:cpp;toolbar: true; auto-links: false;"> main.m// commanddemo//// created by tony on 15/8/13. Copyright (c) 2015 Year 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]; &nbsP; invoker.command = command; [invoker executeCommand]; } return 0;}</pre> <p><p>Running:</p></p> <pre class="brush:cpp;toolbar: true; auto-links: false;"><pre class="brush:cpp;toolbar: true; auto-links: false;">2015-08-13 22:49:56.412 commanddemo[1385:43303] Actual execution</pre></pre> <p><p><br></p></p><p><p>iOS design Mode---command mode (not finished)</p></p></span>