執行個體講解設計模式中的命令模式在iOS App開發中的運用_IOS

來源:互聯網
上載者:User

命令模式封裝一個請求或行為作為一個對象。封裝的請求比原的更加靈活,可以在對象之間傳遞,儲存,動態修改,或放入一個隊列。

那麼讓我們簡要的說一下命令模式的特點。

  • 它能比較容易地設計一個命令隊列;
  • 在需要的情況下,可以較容易地將命令記入日誌;
  • 允許接收請求地一方決定是否要否決請求;
  • 可以容易地實現對請求地撤銷和重做;
  • 由於加進新地具體命令類不影響其他的類,因此增加新的具體命令類很容易;
  • 把請求一個操作的對象與知道怎麼執行一個操作的對象分隔開。

下面給出基本的類結構圖:

上面這張圖是命令模式的類結構的基本圖。其實從這張圖中還可以擴充出很多,細節就不說了,給大家留一些想象的空間,呵呵!

還是老規矩,下面給出執行個體:

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:

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:

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)進行瞭解耦。具體實現過程根據不同的命令進行了區分。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.