Ios development: Agent design mode and ios development design mode
Proxy is a simple and powerful design mode. This mode is used for an object to "represent" another object to do and implement something. The primary object maintains a reference of a proxy and sends a message to the proxy when appropriate. This notification notifies the "proxy" object that is about to process or that an event has been processed. This proxy can respond to the message of the event sent by the primary object by updating the UI interface of itself or other objects or other States. Or, in some cases, a value can be returned to affect how other upcoming events are handled. The main value of proxy is that it allows you to easily customize the behavior of various objects.
For your convenience, I will give you a simple example here: a family has a child who will not eat yet, so when I am hungry, I need my mother to feed me. Here we regard the child as a primary object, and the mother acts as a proxy object to see the implementation of the proxy mode.
Let's take a look at the code.
TestDelegate. h
@protocol TestDelegate@required-(void)feetBaby;@optional-(void)playWithBaby;@end
Here we define a proxy TestDelegate. Note that the proxy function is divided into two types: @ required and @ optional. The former is a required function, and the latter is a selective function.
Baby. h
#import <Foundation/Foundation.h>#import "TestDelegate.h"@interface Baby : NSObject{ }@property(nonatomic)NSString* name;@property(nonatomic,retain)id<TestDelegate>delegate;+(Baby*)initWithName:(NSString*)str;-(void)hungry;@end
In baby. h, a delegate object is defined and its type is id. Because I don't know which class of object will accept this proxy and implement the proxy function, it is defined as the id type.
Baby. m
#import "Baby.h"@implementation Baby@synthesize name;@synthesize delegate;+(Baby*)initWithName:(NSString *)str{ Baby* baby = [[Baby alloc]init]; baby.name = str; return baby;}-(void)hungry{ NSLog(@"%@ is hungry",self.name); [delegate feetBaby];}@end
In. m wrote the hungry function, because when the child is hungry, he will ask for dinner. Although he will not eat it, he knows that someone will feed him, and he does not know who this person is, therefore, calling [delegate feetBaby] in hungry means to let the object that accepts TestDelegate and implements 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, the mother class accepts the Testdelegate proxy. Note that the form of a proxy is @ interface Mother: NSObject <TestDelegate>.
Mother. m
# Import "Mother. h "@ implementation Mother @ synthesize name; + (Mother *) initWithName :( NSString *) str {Mother * moth = [[Mother alloc] init]; moth. name = str; return moth;}-(void) feetBaby {NSLog (@ "");} @ end
Mother uses the TestDelegate proxy, so it is necessary to implement the feetbaby method. As mentioned earlier, the @ requird modifier must be implemented without an error. It doesn't matter if the @ optional modifier can be implemented or not, next let's take a look at how to write the main function.
Main. m
#import <Foundation/Foundation.h>#import "Baby.h"#import "Mother.h"int main(int argc, const char * argv[]) { @autoreleasepool { Baby*baby = [Baby initWithName:@"bob"]; Mother* mot = [Mother initWithName:@"Lily"]; baby.delegate = mot ; [baby hungry]; } return 0;}
Be sure to pay attention to the following code: baby. delegate = mot; the baby proxy is assigned to mot.