實例講解設(shè)計模式中的命令模式在iOS App開發(fā)中的運用
命令模式封裝一個請求或行為作為一個對象。封裝的請求比原的更加靈活,可以在對象之間傳遞,儲存,動態(tài)修改,或放入一個隊列。
那么讓我們簡要的說一下命令模式的特點。
- 它能比較容易地設(shè)計一個命令隊列;
- 在需要的情況下,可以較容易地將命令記入日志;
- 允許接收請求地一方?jīng)Q定是否要否決請求;
- 可以容易地實現(xiàn)對請求地撤銷和重做;
- 由于加進新地具體命令類不影響其他的類,因此增加新的具體命令類很容易;
- 把請求一個操作的對象與知道怎么執(zhí)行一個操作的對象分隔開。
下面給出基本的類結(jié)構(gòu)圖:
上面這張圖是命令模式的類結(jié)構(gòu)的基本圖。其實從這張圖中還可以擴展出很多,細節(jié)就不說了,給大家留一些想象的空間,呵呵!
還是老規(guī)矩,下面給出實例:
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(@"實際執(zhí)行");
}
@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:
2015-08-13 22:49:56.412 CommandDemo[1385:43303] 實際執(zhí)行
Cocoa Touch框架中的命令模式:
NSInvocation對象
如下示例,Client沒有直接調(diào)用Receiver的方法,而是用NSInvocation對象封裝了運行時庫向Receiver發(fā)送執(zhí)行消息所需的所有必要信息,這里的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的實例創(chuàng)建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]; //通過調(diào)用NSInvocation對象的invoke方法,完成對Receiver中action的調(diào)用
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, 28 years old. 2015-08-14 13:37:44.164 InvocationDemo[1049:36632] returnValue: 119
其實,單從類關(guān)系圖中可以簡單的看出,命令模式其實是把需求(Invoker)和具體實現(xiàn)(Receiver)通過命令層(Command)進行了解耦。具體實現(xiàn)過程根據(jù)不同的命令進行了區(qū)分。
- 使用設(shè)計模式中的Singleton單例模式來開發(fā)iOS應(yīng)用程序
- 實例講解如何在iOS應(yīng)用開發(fā)中使用設(shè)計模式中的代理模式
- IOS設(shè)計模式之組合設(shè)計模式
- iOS應(yīng)用設(shè)計模式開發(fā)中對簡單工廠和工廠方法模式的運用
- IOS觀察者設(shè)計模式
- 詳解iOS應(yīng)用的設(shè)計模式開發(fā)中Mediator中介者模式的使用
- 深入解析iOS應(yīng)用開發(fā)中對設(shè)計模式中的橋接模式的使用
- iOS應(yīng)用運用設(shè)計模式中的Strategy策略模式的開發(fā)實例
- iOS應(yīng)用開發(fā)中運用設(shè)計模式中的組合模式的實例解析
- iOS App開發(fā)中使用設(shè)計模式中的單例模式的實例解析
相關(guān)文章
iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件
這篇文章主要介紹了iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11淺談iOS 數(shù)據(jù)結(jié)構(gòu)之鏈表
這篇文章主要介紹了淺談iOS 數(shù)據(jù)結(jié)構(gòu)之鏈表,本文詳細的介紹了單鏈表和雙鏈表,具有一定的參考價值,有興趣的可以了解一下2017-09-09剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作
這篇文章主要介紹了剖析iOS開發(fā)中Cocos2d-x的內(nèi)存管理相關(guān)操作,Cocos2d-x是開發(fā)游戲的利器,需要的朋友可以參考下2015-10-10iOS runtime forwardInvocation詳解及整理
這篇文章主要介紹了 iOS runtime forwardInvocation詳解及整理的相關(guān)資料,需要的朋友可以參考下2017-02-02IOS UI學習教程之設(shè)置UITextField各種屬性
這篇文章主要為大家詳細介紹了IOS UI學習教程之設(shè)置UITextField各種屬性,感興趣的小伙伴們可以參考一下2016-03-03