iOS common design mode-command design mode

Source: Internet
Author: User

Detailed Command Design Patterns

    • Detailed Command Design Patterns
      • Basic concepts
      • Use of Nsinvocation
      • The embodiment of the command pattern

Basic concepts

The command design pattern encapsulates a request or action as an object. This encapsulation request is more flexible than the original request and can be passed, stored, dynamically modified, or put into the queue before the object. Apple's implementation of this model uses target-action mechanisms and invocation.

Use of Nsinvocation

There are 2 ways to call an object directly in iOS

One is Performselector:withobject: Another is nsinvocation the first way is relatively simple, can complete a simple call. But for >2 parameters or the handling of return values, there is a need to do some extra work to get it done. So in this case, we can use nsinvocation to do these relatively complex operations.
Nsinvocation can handle parameters, return values. Everyone in Java knows the reflection operation, in fact nsinvocation is equivalent to the reflection operation.

intMain (intargcConst Char* argv[]) {NSAutoreleasePool* Pool = [[NSAutoreleasePoolALLOC] init]; MyClass *myclass = [[MyClass alloc] init];NSString*mystring = @"My string";//Normal call    NSString*normalinvokestring = [MyClass appendmystring:mystring];NSLog(@"The normal Invoke string is:%@", normalinvokestring);//nsinvocation CallSEL 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= [SuperINIT];if( Self) {//initialization code here.}return  Self;} - (NSString*) Appendmystring: (NSString*) string{NSString*mstring = [NSStringstringwithformat:@"%@ after Append method", string];returnmstring;} - (void) dealloc{[SuperDealloc];}@end

Here is a description of [myinvocation setargument: &mystring atindex:2]; Why is index starting from 2 because: 0 12 parameters have been occupied by target and selector.

The embodiment of the command pattern
    nsmethodsignature*sig = [ Self Methodsignatureforselector:@selector(Addalbum:Atindex:)];nsinvocation*undoaction = [nsinvocationinvocationwithmethodsignature: Sig]; [UndoActionsettarget: Self]; [UndoActionSetselector:@selector(Addalbum:Atindex:)]; [UndoActionsetargument:&deletedalbumAtindex:2]; [UndoActionsetargument:&currentalbumindexAtindex:3];       [UndoAction retainarguments]; [UndostackAddObject:UndoAction]; -(void) UndoAction {if(Undostack.count >0)       {nsinvocation*undoaction = [Undostack lastobject];           [Undostack Removelastobject];       [UndoAction invoke]; }} Undo action pops up the top of the stacknsinvocationObject, and then invokes it via invoke

iOS common design mode-command design mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.