iOS development: Proxy design mode

Source: Internet
Author: User

Http://www.cnblogs.com/zyi1992/p/4509908.html

Reprinted, studied the

A proxy is a simple and powerful design pattern that is used by an object to "represent" another object to do and implement something. The main object maintains a reference to a proxy (delegate) and sends a message to the agent at the appropriate time, which notifies the "agent" that the object is about to be processed or that an event has been processed. The agent can respond to the message of the event sent by the main object by updating the UI interface or other state of the object. Or, in some cases, return a value to affect how other upcoming events should be handled. The main value of the agent is that it allows you to easily customize the behavior of various objects.

To make it easy for everyone to understand, I'm here to give a simple example of a situation where a family has a child who is small enough to eat, so she needs a mother to feed when he is hungry. Here we regard the child as a main object, mother as a proxy object to look at the implementation of proxy mode.

Take a direct look at the code

TestDelegate.h

@protocol testdelegate@required-(void) feetbaby; @optional-(void) Playwithbaby; @end

Here defines a proxy testdelegate notice that the proxy functions are divided into @required and @optional Two, the former is a function that must be implemented, the latter is a selective implementation of the function

Baby.h

#import <Foundation/Foundation.h>"TestDelegate.h"@interface Baby: nsobject{    } @property (nonatomic) nsstring* name; @property (nonatomic,retain) ID<testdelegate >delegate; + (baby*) Initwithname: (nsstring*) str; -(void) hungry; @end

A delegate object is defined in Baby.h and its type is ID because the object that does not know which class will accept the proxy and implement the proxy function is defined as the ID type

Baby.m

#import "Baby.h"@implementationBaby@synthesizename;@synthesize Delegate;+ (baby*) Initwithname: (NSString *) str{Baby* Baby =[[Baby alloc]init]; Baby.name=str; returnbaby;}-(void) hungry{NSLog (@"%@ is hungry", Self.name); [DelegateFeetbaby];}@end

In the BABY.M wrote the Hungry function, because the child when hungry will be called to eat, although they will not eat, but know that someone will come to feed him, and do not know who this person is, so in hungry call [delegate feetbaby]; meaning to accept Testdel Egate and implements the object of the Feetbaby method to feed the child

 

Mother.h

#import <Foundation/Foundation.h>#import"TestDelegate.h"  @interface mother:nsobject<testdelegate>{    } @property (nonatomic) nsstring* name; + (mother*) Initwithname: (nsstring*) str; @end

Obviously here mother this class accepts testdelegate this agent is aware that a proxy is used in the form of a @interface mother:nsobject<testdelegate>

Mother.m

  #import  "     @implementation   mother   @synthesize   name;  + (mother*) Initwithname: (nsstring *) str{mother  * Moth = [[Mother Alloc]init];    Moth.name  = str;  return   moth;}  -(void  ) feetbaby{NSLog ( @ "  start feeding the kids  "   );}  @end  

Mother use testdelegate this agent so to implement the Feetbaby method, the previous said @requird decoration method is must be implemented to not error, @optional modification can choose to implement does not matter, Here's a look at how the main function is written

Main.m

#import<Foundation/Foundation.h>#import "Baby.h"#import "Mother.h"intMainintargcConst Char*argv[]) {@autoreleasepool {Baby*baby = [Baby initwithname:@"Bob"]; Mother* mot = [mother initwithname:@"Lily"]; Baby.Delegate=mot;          [Baby hungry]; }    return 0;}

Be sure to note here baby.delegate = mot; This code, realized the baby's proxy gave MOT.

iOS development: Proxy 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.