The command mode encapsulates a request or behavior as an object. Packaged requests are more flexible than the original, and can be passed, stored, dynamically modified, or placed in a queue between objects.
So let's briefly describe the characteristics of the command pattern.
- It can design a command queue more easily;
- Where necessary, the command can be logged more easily;
- Allow the party receiving the request to decide whether to veto the request;
- Can be easily implemented to withdraw and redo requests;
- Adding new specific command classes is easy because adding new specific command classes does not affect other classes;
- Separates an object that requests an action from an object that knows how to perform an action.
The basic class structure diagram is given below:
The above diagram is the basic diagram of the class structure of the command mode. In fact, from this picture can also expand a lot of details do not say, give everyone to leave some imaginary space, hehe!
Or the old rules, here's an example:
objective-c Example:
Command:
Copy Code code as follows:
//
NimoCommand.h
Commanddemo
//
#import <Foundation/Foundation.h>
@protocol Nimocommand <NSObject>
-(void) execute;
@end
Concretecommand:
Copy Code code as follows:
//
NimoConcreteCommand.h
Commanddemo
//
#import <Foundation/Foundation.h>
#import "NimoCommand.h"
@class Nimoreceiver;
@interface Nimoconcretecommand:nsobject <NimoCommand>
@property (nonatomic) Nimoreceiver *receiver;
-(ID) Initwithreceiver: (Nimoreceiver *) receiver;
@end
Copy Code code as follows:
//
Nimoconcretecommand.m
Commanddemo
//
#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:
Copy Code code as follows:
//
NimoReceiver.h
Commanddemo
//
#import <Foundation/Foundation.h>
@interface Nimoreceiver:nsobject
-(void) action;
@end
Copy Code code as follows:
//
Nimoreceiver.m
Commanddemo
//
#import "NimoReceiver.h"
@implementation Nimoreceiver
-(void) action
{
NSLog (@ "actual execution");
}
@end
Invoker:
Copy Code code as follows:
//
NimoInvoker.h
Commanddemo
//
#import <Foundation/Foundation.h>
#import "NimoCommand.h"
@interface Nimoinvoker:nsobject
@property (nonatomic, weak) id<nimocommand> command;
-(void) ExecuteCommand;
@end
Copy Code code as follows:
//
Nimoinvoker.m
Commanddemo
//
#import "NimoInvoker.h"
@implementation Nimoinvoker
-(void) ExecuteCommand
{
[_command execute];
}
@end
Client:
Copy Code code as follows:
//
Main.m
Commanddemo
//
#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] Actual implementation
Cocoa the command mode in the touch frame:
Nsinvocation objects
In the following example, the client does not call the receiver method directly, but instead uses the Nsinvocation object to encapsulate all the necessary information that the Run-time library needs to send execution messages to receiver. The Nsinvocation object here is similar to the Concretecommand object above.
Receiver:
Copy Code code as follows:
//
NimoReceiver.h
Invocationdemo
//
#import <Foundation/Foundation.h>
@interface Nimoreceiver:nsobject
-(int) Printwithname: (NSString *) name Gender: (NSString *) Gender Age: (int) age;
@end
Copy Code code as follows:
//
Nimoreceiver.m
Invocationdemo
//
#import "NimoReceiver.h"
@implementation Nimoreceiver
-(int) Printwithname: (NSString *) name Gender: (NSString *) Gender Age: (int) Age
{
NSLog (@ "My name are%@,%@,%d years old.", name, gender, age);
return 119;
}
@end
Client:
Copy Code code as follows:
//
Main.m
Invocationdemo
//
#import <Foundation/Foundation.h>
#import "NimoReceiver.h"
int main (int argc, const char * argv[]) {
@autoreleasepool {
Creates a Nsinvocation object with an instance of receiver and takes the action of receiver as a selector
Nimoreceiver *receiver = [[Nimoreceiver alloc] init];
NSString *name = @ "Lee";
NSString *gender = @ "male";
int age = 28;
Sel sel = @selector (printWithName:gender:age:);
Nsmethodsignature *methodsignature = [[Receiver class] instancemethodsignatureforselector:sel];
Nsinvocation *invocation = [Nsinvocation invocationwithmethodsignature:methodsignature];
[Invocation settarget:receiver];
[Invocation Setselector:sel];
[Invocation setargument:&name atindex:2];
[Invocation Setargument:&gender Atindex:3];
[Invocation setargument:&age Atindex:4];
[Invocation retainarguments];
[Invocation invoke]; Completes the call to the action in receiver by invoking the Invoke method of the Nsinvocation object
int returnvalue = 0;
[Invocation getreturnvalue:&returnvalue];
NSLog (@ "returnvalue:%d", returnvalue);
}
return 0;
}
Running:
2015-08-14 13:37:44.162 invocationdemo[1049:36632] My name is Lee, male, years old.
2015-08-14 13:37:44.164 invocationdemo[1049:36632] returnvalue:119
In fact, it is simple to see from the class diagram that the command pattern is actually a decoupling of the requirements (invoker) and the implementation (Receiver) through the command layer. The specific implementation process is differentiated according to different commands.