iOS design mode-Proxy
Schematic diagram
Description
1. Proxy mode Everyone has used it, but using abstract base class Nsproxy to implement agent estimation fresh
2. I use Nsproxy to implement the proxy mode, for understanding the message forwarding mechanism a little help
Source
Https://github.com/YouXianMing/ProxyPattern
////AbstractProxy.h//Appproxy////Created by youxianming on 15/8/4.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>@interfaceAbstractproxy:nsproxy/** * Proxy object*/@property (nonatomic, weak)IDcustomer;/** * Proxy Client * * @param customer has implemented some kind of agreement client * * @return proxy object*/-(Instancetype) Initwithcustomer: (ID) customer;@end
////abstractproxy.m//Appproxy////Created by youxianming on 15/8/4.//Copyright (c) 2015 youxianming. All rights reserved.//#import<objc/runtime.h>#import "AbstractProxy.h"#import "AbstractExcute.h"@implementationAbstractproxy-(Instancetype) Initwithcustomer: (ID) Customer {Self.customer=customer; returnSelf ;}#pragmamark-nsproxy-(Nsmethodsignature *) Methodsignatureforselector: (SEL) Aselector {if([Self.customer respondstoselector:aselector]) {return[Self.customer Methodsignatureforselector:aselector]; } Else{Abstractexcute*excute =[Abstractexcute shareinstance]; return[Excute methodsignatureforselector:nsselectorfromstring (@"Nullexcute:")]; }}- (void) Forwardinvocation: (Nsinvocation *) Invocation {SEL selector=[invocation selector]; if([Self.customer respondstoselector:selector]) {[Invocation setTarget:self.customer]; [Invocation invoke]; } Else{nsstring*selectorstring =Nsstringfromselector (Invocation.selector); Invocation.selector= Nsselectorfromstring (@"Nullexcute:"); Abstractexcute*excute =[Abstractexcute shareinstance]; [Invocation Settarget:excute]; Const Char*classname = Class_getname ([selfclass]); Nsarray*classnamearray =Nil; if(self.customer) {Classnamearray= @[[nsstring Stringwithutf8string:classname], selectorstring,@""]; } Else{Classnamearray=@[[nsstring Stringwithutf8string:classname], selectorstring]; } [invocation setargument:&classnamearray Atindex:2]; [Invocation invoke]; }}@end
// // AbstractExcute.h// appproxy//// Created by Youxianming on 15/8/4. // Copyright (c) 2015 youxianming. All rights reserved. // #import <Foundation/Foundation.h>@interface abstractexcute:nsobject+ (instancetype) shareinstance; @end
////ABSTRACTEXCUTE.M//Appproxy////Created by youxianming on 15/8/4.//Copyright (c) 2015 youxianming. All rights reserved.//#import "AbstractExcute.h"@implementationAbstractexcute+(instancetype) shareinstance {StaticAbstractexcute *sharedabstractexcute =Nil; Staticdispatch_once_t predicate; Dispatch_once (&predicate, ^{Sharedabstractexcute=[[Self alloc] init]; }); returnSharedabstractexcute;}- (void) Nullexcute: (Nsarray *) ClassName {if(Classname.count = =3) {NSLog (@"%@ Agent is set, but the agent does not implement the%@ method", classname[0], classname[1]); } Else{NSLog (@"%@ No proxy set, method%@ not executed", classname[0], classname[1]); } }@end
Details
The key to the implementation of the Nsproxy sub-class
iOS design mode-Proxy