iOS常用設計模式——命令設計模式

來源:互聯網
上載者:User

標籤:命令設計模式

命令設計模式詳解

  • 命令設計模式詳解
    • 基本概念
    • NSInvocation的使用
    • 命令模式的體現

基本概念

命令設計模式將一個請求或行動作封裝為對象。這個封裝請求比原始的請求要靈活並且可以在對象之前被傳遞,儲存,動態修改或者放進隊列裡面。蘋果公司實現這種模式使用Target-Action機制和Invocation。

NSInvocation的使用

在 iOS中可以直接調用 某個對象的訊息 方式有2種

一種是performSelector:withObject:再一種就是NSInvocation第一種方式比較簡單,能完成簡單的調用。但是對於>2個的參數或者有傳回值的處理,那就需要做些額外工作才能搞定。那麼在這種情況下,我們就可以使用NSInvocation來進行這些相對複雜的操作
NSInvocation可以處理參數、傳回值。會java的人都知道反射操作,其實NSInvocation就相當於反射操作。

int main (int argc, const char * argv[]){    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    MyClass *myClass = [[MyClass alloc] init];    NSString *myString = @"My string";    //普通調用    NSString *normalInvokeString = [myClass appendMyString:myString];    NSLog(@"The normal invoke string is: %@", normalInvokeString);    //NSInvocation調用    SEL mySelector = @selector(appendMyString:);    NSMethodSignature * sig = [[myClass class]                        instanceMethodSignatureForSelector: mySelector];    NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature: sig];    [myInvocation setTarget: myClass];    [myInvocation setSelector: mySelector];    [myInvocation setArgument: &myString atIndex: 2];    NSString * result = nil;        [myInvocation retainArguments];        [myInvocation invoke];    [myInvocation getReturnValue: &result];    NSLog(@"The NSInvocation invoke string is: %@", result);    [myClass release];    [pool drain];    return 0;}MyClass.h#import <Foundation/Foundation.h>@interface MyClass : NSObject {    }- (NSString *)appendMyString:(NSString *)string;@endMyClass.m#import "MyClass.h"@implementation MyClass- (id)init{    self = [super init];    if (self) {        // Initialization code here.    }    return self;}- (NSString *)appendMyString:(NSString *)string{    NSString *mString = [NSString stringWithFormat:@"%@ after append method", string];    return mString;}- (void)dealloc{    [super dealloc];}@end

這裡說明一下[myInvocation setArgument: &myString atIndex: 2];為什麼index從2開始 ,原因為:0 1 兩個參數已經被target 和selector佔用。

命令模式的體現
    NSMethodSignature *sig = [self methodSignatureForSelector:@selector(addAlbum:atIndex:)];       NSInvocation *undoAction = [NSInvocationinvocationWithMethodSignature:sig];       [undoAction setTarget:self];       [undoAction setSelector:@selector(addAlbum:atIndex:)];       [undoAction setArgument:&deletedAlbum atIndex:2];       [undoAction setArgument:&currentAlbumIndex atIndex:3];       [undoAction retainArguments];       [undoStack addObject:undoAction];   - (void)undoAction   {      if (undoStack.count > 0)       {           NSInvocation *undoAction = [undoStack lastObject];           [undoStack removeLastObject];           [undoAction invoke];       }    }撤銷操作彈出棧頂的NSInvocation對象,然後通過invoke調用它

iOS常用設計模式——命令設計模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.