命令模式封装一个请求或行为作为一个对象。封装的请求比原的更加灵活,可以在对象之间传递,储存,动态修改,或放入一个队列。
那么让我们简要的说一下命令模式的特点。
- 它能比较容易地设计一个命令队列;
- 在需要的情况下,可以较容易地将命令记入日志;
- 允许接收请求地一方决定是否要否决请求;
- 可以容易地实现对请求地撤销和重做;
- 由于加进新地具体命令类不影响其他的类,因此增加新的具体命令类很容易;
- 把请求一个操作的对象与知道怎么执行一个操作的对象分隔开。
下面给出基本的类结构图:
上面这张图是命令模式的类结构的基本图。其实从这张图中还可以扩展出很多,细节就不说了,给大家留一些想象的空间,呵呵!
还是老规矩,下面给出实例:
objective-c 示例:
command:
//
// nimocommand.h
// commanddemo
//
#import <foundation/foundation.h>
@protocol nimocommand <nsobject>
- (void)execute;
@end
concretecommand:
//
// 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
//
// 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:
//
// nimoreceiver.h
// commanddemo
//
#import <foundation/foundation.h>
@interface nimoreceiver : nsobject
- (void)action;
@end
//
// nimoreceiver.m
// commanddemo
//
#import "nimoreceiver.h"
@implementation nimoreceiver
- (void)action
{
nslog(@"实际执行");
}
@end
invoker:
//
// nimoinvoker.h
// commanddemo
//
#import <foundation/foundation.h>
#import "nimocommand.h"
@interface nimoinvoker : nsobject
@property (nonatomic, weak) id<nimocommand> command;
- (void)executecommand;
@end
//
// nimoinvoker.m
// commanddemo
//
#import "nimoinvoker.h"
@implementation nimoinvoker
- (void)executecommand
{
[_command execute];
}
@end
client:
//
// 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:
1
|
2015-08-13 22:49:56.412 commanddemo[1385:43303] 实际执行
|
cocoa touch框架中的命令模式:
nsinvocation对象
如下示例,client没有直接调用receiver的方法,而是用nsinvocation对象封装了运行时库向receiver发送执行消息所需的所有必要信息,这里的nsinvocation对象类似于上文中的concretecommand对象。
receiver:
//
// nimoreceiver.h
// invocationdemo
//
#import <foundation/foundation.h>
@interface nimoreceiver : nsobject
- (int)printwithname:(nsstring *)name gender:(nsstring *)gender age:(int)age;
@end
//
// nimoreceiver.m
// invocationdemo
//
#import "nimoreceiver.h"
@implementation nimoreceiver
- (int)printwithname:(nsstring *)name gender:(nsstring *)gender age:(int)age
{
nslog(@"my name is %@, %@, %d years old.", name, gender, age);
return 119;
}
@end
client:
//
// main.m
// invocationdemo
//
#import <foundation/foundation.h>
#import "nimoreceiver.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//用receiver的实例创建nsinvocation对象,并把receiver的action作为选择器
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]; //通过调用nsinvocation对象的invoke方法,完成对receiver中action的调用
int returnvalue = 0;
[invocation getreturnvalue:&returnvalue];
nslog(@"returnvalue: %d", returnvalue);
}
return 0;
}
running:
1
2
|
2015-08-14 13:37:44.162 invocationdemo[1049:36632] my name is lee, male, 28 years old.
2015-08-14 13:37:44.164 invocationdemo[1049:36632] returnvalue: 119
|
其实,单从类关系图中可以简单的看出,命令模式其实是把需求(invoker)和具体实现(receiver)通过命令层(command)进行了解耦。具体实现过程根据不同的命令进行了区分。