In the previous article we introduced the concept of the Protocol in OC: HTTP://BLOG.CSDN.NET/JIANGWEI0910410003/ARTICLE/DETAILS/41776015, this article is about the agent mode in OC, About the agent mode, if there are students are not too clear, they have to add knowledge, here do not introduce, here only to introduce how OC is the implementation of proxy mode.
Here is a simple example:
Kids, nurses, nannies, kids there are two ways: wash and play
Here the Agent object is: Nurse class, Nanny class, child class is the agent object.
Check the code:
First look at the kids Category:
Children.h
children.h// 12_designstyle//// Created by Jiangwei on 14-10-11.// Copyright (c) 2014 Jiangwei. All rights reserved.//#import <Foundation/Foundation.h> @class children;//If you don't have this line of code, Protocol Childrendelegate Children type will not be found, error @protocol childrendelegate <NSObject> @required-(void) Wash: (Children *) children;-(void) Play: (Children *) children; @end @interface children:nsobject{//nure *_nure;//nanny // This can be accomplished by using polymorphic techniques, because nannies, nurses have a common parent class nsobject, but this is not the way to use it, but instead uses the ID type //But we also need to add some methods for this type, here is the Protocol // This proxy object must conform to the Childrendelegate protocol id<childrendelegate> _delegate;//This variable is the child's proxy object Nsinteger timevalue;} -(void) Setdelegate: (id) delegate; @end
Here, we define a protocol: Childrendelegate, he has two necessary methods: wash and Play
We also define a very important property.
_delegate
This attribute definition is a bit different, this is the essence of implementing the proxy object, the ID is an indeterminate type, so the _delegate variable can be assigned to the type is:
As long as the class of the Childrendelegate protocol is implemented. It is remembered here that there is a lot more to be done later in this definition method. Equivalent to the interface type in Java, only the type of its implementation class can be assigned. Just here the definition format is:id< protocol name >
Then there is a way to set up the protocol, note that the parameter type must also be an ID
In fact, this is also involved in the previously mentioned polymorphic characteristics, so that the proxy mode is also based on the characteristics of polymorphism.
Children.m
children.m// 12_designstyle//// Created by Jiangwei on 14-10-11.// Copyright (c) 2014 Jiangwei. All rights reserved.//#import "Children.h"//Here are some of the nanny's actions//If you want to call a nurse now, then we have to go to the new nurse, then this code needs to change, the nanny's place to replace the nurse's place/ The reason is because the coupling is too high, the nanny and the child coupling together, if need change, you need to change the code//@implementation children-(ID) init{self = [super init]; if (self! = nil) { [nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (timeraction:) userInfo : nil Repeats:yes]; } return self;} -(void) Setdelegate: (ID) delegate{ _delegate = delegate;} -(void) Timeraction: (Nstimer *) timer{ timevalue++; if (TimeValue = = 5) { [_delegate wash:self]; } if (TimeValue = = ten) { [_delegate play:self];} } @end
We have customized an initialization method, and we have done a timer in the initialization method.
[Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (timeraction:) Userinfo:nil Repeats:yes];
This is the start of a simple timer in oc: every 1s to execute the timeraction method in self.
The timer in OC is not the same as Java, and his execution logic can be done separately in a specified method (the function pointer in C is similar, as long as a function pointer is passed, you can execute the specified function, so @selector does the job), However, Java must implement the Runable interface to execute the specified logic code in the Run method.
In the Timeraction method, we determine that when the time is 5s, the Wash method executes the agent object, and the play method is executed at 10s.
Take a look at the nurse class:
Nure.h
nure.h// 12_designstyle//// Created by Jiangwei on 14-10-11.// Copyright (c) 2014 Jiangwei. All rights reserved.//#import <Foundation/Foundation.h> #import "Children.h" @interface nure:nsobject< childrendelegate>-(void) Wash: (Children *) children;-(void) Play: (Children *) children; @end
The nurse class is simple enough to implement the Childrendelegate protocol
Nure.m
nure.m// 12_designstyle//// Created by Jiangwei on 14-10-11.// Copyright (c) 2014 Jiangwei. All rights reserved.//#import "Nure.h" #import "Children.h" @implementation nure-(void) Wash: (Children *) children{ NSLog (@ "The child is dirty, the babysitter helps the child bathe"); -(void) Play: (Children *) children{ NSLog (@ "The child cries, the babysitter and the Child play"); @end
This is where the wash and play methods are implemented.
Look at the Nanny class:
Nanny.h
nanny.h// 12_designstyle//// Created by Jiangwei on 14-10-11.// Copyright (c) 2014 Jiangwei. All rights reserved.//#import <Foundation/Foundation.h> #import "Children.h" @interface nanny:nsobject< childrendelegate>-(void) Wash: (Children *) children;-(void) Play: (Children *) children; @end
nanny.m
nanny.m// 12_designstyle//// Created by Jiangwei on 14-10-11.// Copyright (c) 2014 Jiangwei. All rights reserved.//#import "Nanny.h" #import "Children.h" @implementation nanny-(void) Wash: (Children *) children{ NSLog (@ "The child is dirty, the nurse helps the child bathe"); -(void) Play: (Children *) children{ NSLog (@ "The child cries, the nurse and the child play"); @end
The code logic for the Nanny class and the Nurse class is the same, because both of them are implementing a protocol
Test class
Main.m
main.m// 12_designstyle//// Created by Jiangwei on 14-10-11.// Copyright (c) 2014 Jiangwei. All rights reserved.//#import <Foundation/Foundation.h> #import "Children.h" #import "Nure.h" #import "Nanny.h"// Core: ID Type + protocol//Do low-coupling operation//can also do communication between two classes int main (int argc, const char * argv[]) { @autoreleasepool { Children * Child = [[Children alloc] init]; Nure *nure = [[Nure alloc] init]; Nanny *nanny= [[Nanny alloc] init]; [Child setdelegate:nanny];// [child setnure:nure]; [[Nsrunloop Currentrunloop] run]; } return 0;}
See, the test class is simple. We also found that the advantages of proxy mode is also revealed, such as now another person to take care of the children: Mother class, then we just let the mother class implementation of the agreement can be. This coupling level is not very high either. So the proxy mode is super useful, and when we develop iOS later, we find that there are a lot of proxy patterns in it.
Operation Result:
Summarize
This article introduces how to implement proxy mode in OC, in fact, the core technology of Agent mode in OC is:ID type + protocol + polymorphism
---agent mode of OC Learning article