"Original" to talk about the misunderstanding of agent mode in Objective-c
Please specify the source of the article reproduced-- polobymulberry-Blog Park
1. Preface
This article mainly compares the proxy mode and the delegate mode, and personally thinks that most of the delegate usage in objective-c belongs to the delegate mode. The paper has some key concepts and has no effect on the actual development.
Some time ago to see a blog iOS development-from a topic to see delegate, and this blog iOS APP architecture to solve the problem similar. Two blogs are well written to solve the data transfer problem between two pages:
A page has a Uilabel *labela,b page that has a Uitextfield *textfieldb. After jumping from page A to page B, changing the data in Textfieldb and then returning to page A, Labela will show the changed data in Textfieldb, well, that's a simple data transfer scenario.
There are many ways to solve this problem, such as using a DAO (data Access object) to maintain the data corresponding to Labela and TEXTFIELDB. The data flow to the page is as follows:
But this scenario is not very complex, so there is no need to introduce such a heavy architecture to DAO.
Sometimes we fall into the details of the technology can not extricate themselves, may wish to calm down to think about, what is the nature of this problem?
The difficulty with this problem is that textfieldb in page B cannot be notified of Labela in page A after the data changes. If you have a Labela reference in page B, then you can manipulate labela directly in the code of page B. So I added a uilabel *labelaref in page b, when a page push to page B, page A of the Labela assigned to Labelref can (pro-test data transfer).
The above method is feasible, but we must all feel that the design is too rough. If the data is delivered in more business, then page B needs to refer to many of the properties of page A. Of course, we can directly refer to page A as the property of page B, that is, Uiviewcontroller *VCA. As shown in the following:
There's nothing wrong with this design. However, our topic is the proxy mode, then we say this question in the end and the agency model what is the connection?
2. Using proxy mode for data transfer
Let's take a look at Gof. Description of the proxy pattern in design mode: Reusable software-oriented Foundation: provides a proxy for other objects to control access to this object. Hey, is it like the above question? provides a proxy for page B to control access to page a , you can control page A, then you can control the Labela in page a. But the above method of directly referencing objects can also provide access to this object ah, why must pass proxy it? Let's look at the UML diagram for proxy mode:
Notice that both the proxy and the Realsubject implement the subject interface, and the same interface function doaction () is implemented, and the proxy has a reference to Realsubject, which is the delegate in the diagram. In general, when a proxy implements Doaction, it invokes the doaction of Realsubject, which is the realsubject of doaction called by the referenced delegate. According to my own understanding, the reason for proxy mode is that the user needs to expand the doaction function of the realsubject, and can not directly modify the doaction in Realsubject (and also violate the closed-open principle), So the use of proxy to the realsubject of the doaction has been extended, and the content of the extension is doaction, so the doaction abstracted out, made an interface.
Back in the case above, we can use the proxy mode to design the following architecture:
Here is a small trick, that is, how to identify who is the agent-directly with the client is the agent, where the client is the Viewcontrollerb textfieldb control, so directly dealing with is Viewcontrollerb, That is to say Viewcontrollerb is the agent.
The code is as follows:
Datatransdelegate
// datatransdelegate @protocol Datatransdelegate <nsobject>-(void) didtextfieldchanged: (Uitextfield *) TextField; @end
Viewcontrollera
//VIEWCONTROLLERA.M#import "ViewControllerA.h"#import "ViewControllerB.h"#import "DataTransDelegate.h"@interfaceViewcontrollera () <DataTransDelegate>@property (Strong, nonatomic) UILabel*Labela, @property (Strong, nonatomic) UIButton*Buttona;@end@implementationViewcontrollera- (void) viewdidload {[Super viewdidload]; [Self.view AddSubview:self.labelA]; [Self.view AddSubview:self.buttonA]; [Self.buttona addtarget:self Action: @selector (PUSHVC) forcontrolevents:uicontroleventtouchupinside];}- (void) pushvc{Viewcontrollerb*VCB =[[Viewcontrollerb alloc] init]; VcB.Delegate=Self ; [Self.navigationcontroller PUSHVIEWCONTROLLER:VCB animated:no];}- (void) didtextfieldchanged: (Uitextfield *) textfield{Self.labelA.text=Textfield.text;}-(UILabel *) labela{if(_labela = =Nil) {_labela= [[UILabel alloc] Initwithframe:cgrectmake ( -, -, $, -)]; _labela.text=@"Show TextField content in VCB"; } return_labela;}-(UIButton *) buttona{if(_buttona = =Nil) {_buttona= [[UIButton alloc] Initwithframe:cgrectmake ( -, $, -, -)]; _buttona.backgroundcolor=[Uicolor Bluecolor]; [_buttona settitle:@"Enter VCB"Forstate:uicontrolstatenormal]; } return_buttona;}@end
Viewcontrollerb
//ViewControllerB.h@protocoldatatransdelegate;@interfaceViewcontrollerb:uiviewcontroller@property (nonatomic, weak)ID<DataTransDelegate>Delegate;@end//VIEWCONTROLLER.M#import "ViewControllerB.h"#import "DataTransDelegate.h"@interfaceViewcontrollerb () <uitextfielddelegate, datatransdelegate>@property (Strong, nonatomic) Uitextfield*textfieldb;@end@implementationViewcontrollerb- (void) viewdidload {[Super viewdidload]; [Self.view AddSubview:self.textFieldB]; Self.textfieldb.Delegate=Self ;}- (void) Textfielddidendediting: (Uitextfield *) textfield{[self Didtextfieldchanged:textfield];}- (void) didtextfieldchanged: (Uitextfield *) textfield{[self.DelegateDidtextfieldchanged:textfield];}-(Uitextfield *) textfieldb{if(_textfieldb = =Nil) {_textfieldb= [[Uitextfield alloc] Initwithframe:cgrectmake ( -, -, -, -)]; _textfieldb.text=@"Enter text"; _textfieldb.backgroundcolor=[Uicolor Redcolor]; } return_textfieldb;}@end
The effect is as follows:
3. Misconceptions about proxy mode
In fact, so far there is nothing strange. The key is in everyone to OBJECTIVE-C protocol use, generally is combined with delegate use. Most of us call this mode a proxy mode, but I think delegate is more like a delegate mode than a real agent, proxy is proxies, and delegation is delegate. In addition, proxies and proxies in the proxy mode need to inherit and implement the same interface subject, while we use delegate generally only need to let one of the classes inherit and implement the corresponding interface.
The delegation mode is a basic technique in software design pattern. In delegate mode, there are two objects involved in processing the same request, and the object that accepts the request delegates the request to another object for processing. In fact, the above Viewcontrollerb contains the Viewcontrollera of the reference to this approach is the delegate mode.
For example, our most well-known UITableView, is a typical delegate mode, it will tableview in the unchanged part of the package, will often change the part entrusted to the user to handle, so say UITableView is a delegator, And the class that follows Uitableviewdelegate is delegate, so we often use an expression like self.tableView.delegate = self in a uiviewcontroller;
You may wonder why you need to use Uitableviewdelegate, which is similar to interface in Java? My personal understanding is that because of this convenient unified interface, the interface is unified, convenient for users, because only need to implement these interfaces can be.
So we can see that the two blogs that were mentioned at the very beginning actually rely on the protocol in the OBJECTIVE-C to implement the delegate mode.
If I have to say that the delegation mode and the proxy mode of what relationship, I think the proxy mode should be considered a special delegation mode.
"Original" to talk about the misunderstanding of agent mode in Objective-c