iOS design mode-Proxy

Source: Internet
Author: User

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

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.