黑暗天使巨刃,黑水玄蛇txt,股市春节放假安排2015
命令模式封装一个请求或行为作为一个对象。封装的请求比原的更加灵活,可以在对象之间传递,储存,动态修改,或放入一个队列。
那么让我们简要的说一下命令模式的特点。
下面给出基本的类结构图:
上面这张图是命令模式的类结构的基本图。其实从这张图中还可以扩展出很多,细节就不说了,给大家留一些想象的空间,呵呵!
还是老规矩,下面给出实例:
objective-c 示例:
command:
#import <foundation/foundation.h>
@interface nimoreceiver : nsobject
- (void)action;
@end
#import "nimoreceiver.h"
@implementation nimoreceiver
- (void)action
{
nslog(@"实际执行");
}
@end
#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;
}
2015-08-13 22:49:56.412 commanddemo[1385:43303] 实际执行
cocoa touch框架中的命令模式:
nsinvocation对象
如下示例,client没有直接调用receiver的方法,而是用nsinvocation对象封装了运行时库向receiver发送执行消息所需的所有必要信息,这里的nsinvocation对象类似于上文中的concretecommand对象。
receiver:
#import <foundation/foundation.h>
@interface nimoreceiver : nsobject
- (int)printwithname:(nsstring *)name gender:(nsstring *)gender age:(int)age;
@end
#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
#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;
}
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)进行了解耦。具体实现过程根据不同的命令进行了区分。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
网友评论