iOS Development-Proxy mode

Source: Internet
Author: User

Proxy mode is sometimes referred to as the delegate mode, but in fact there is a difference between the two, the proxy mode provides an alias or placeholder for another object to access the object, the proxy object and the control access object belong to the same class, and the delegate object and object do not necessarily belong to the same class. Both can control the access of the class, the Access Proxy method A also means access to the object's method A, access to the delegate object method A is the object can be a method B. From the actual development point of view, the delegation belongs to the expanded version of the agency model, and there are not so many restrictions.

Basic Knowledge

Proxy mode is relatively simple, you can simply look at the UML class diagram:

Proxy mode to manage client access to objects, there are a number of ways to manage access. The remote agent manages the interaction between the client and the remote object, the virtual agent controls the instance overhead object, the proxy pattern is somewhat similar to the adorner pattern, and the adorner pattern acts as an object, and the proxy mode Controls access to the other object.

Practical Research

We will write a more general example based on the UML class diagram above, defining the definition subject first:

@protocol subjectprotocol<nsobject>-(void) displayimage; @end @interface Subject:nsobject<subjectprotocol > @end

The Subject property in Proxy is equivalent to a reference to a real object in UML delegate:

@interface Subjectproxy () @property (strong,nonatomic)  Subject  *subject; @property (strong,nonatomic)  NSString  *filename; @end @implementation subjectproxy-(instancetype) Initwithfilename: (NSString *) fileName{    self=[super init];    if (self) {        self.filename=filename;    }    return self;} -(void) displayimage{    if (!self.subject) {        self.subject=[[realsubject alloc]initwithfilename: Self.filename];    }    [Self.subject displayimage];} @end

Implementation of Realsubject:

@interface Realsubject () @property (strong,nonatomic) nsstring *filename; @end @implementation realsubject-( Instancetype) Initwithfilename: (NSString *) filename{    self=[super init];    if (self) {        self.filename=filename;        }    return self;} -(void) loadfromdisk{    NSLog (@ "loading--%@", Self.filename);} -(void) displayimage{    NSLog (@ "display--%@", Self.filename);} @end

Call:

    Subject *sub=[[subjectproxy alloc]initwithfilename:@ "blog Park-flyelephant"];    Subject *proxy=[[subjectproxy alloc]initwithfilename:@ "book"];    [Sub displayimage];    [Proxy displayimage];

Output:

2016-01-19 08:22:28.305 ios[24822:5266030] display--Blog Park-flyelephant2016-01-19 08:22:28.305 iOS[24822:5266030] Display--subject---Book

In iOS development, we use a lot of delegates, understand the proxy mode is also very easy, agent in Objective-c has a more important example is used to reference the Count pointer object, when a complex object multiple copies must exist, proxy mode can be combined with the use of meta-mode to reduce memory usage. A typical practice is to create a complex object and multiple proxies, each of which will refer to the original complex object. And the operation of the agent is transferred to the original object. Complex objects are removed once all agents are not present.

about delegates in iOS if you are unfamiliar, you can refer to the iOS development before this article -entrust Combat ~

iOS Development-Proxy 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.