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:¤talbumindexAtindex: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