[Original] I will talk about the misunderstanding of the proxy mode in Objective-C, and the misunderstanding of objective-c.

Source: Internet
Author: User

[Original] I will talk about the misunderstanding of the proxy mode in Objective-C, and the misunderstanding of objective-c.
[Original] misunderstandings about the proxy mode in Objective-C

Please specify the source for this reprinted article --Polobymulberry-blog

1. Preface

This article mainly compares the proxy mode and delegation mode. I personally think that most of the delegate usage in Objective-C belongs to the delegation mode.The full text has some concepts of timeline, which has no impact on actual development.

A blog iOS development I saw some time ago -- Delegate from a single question is similar to the problem solved by this blog's iOS APP architecture. Both blogs are well written to solve the data transmission problem between two pages:

There is A UILabel * labelA in page A, and A UITextField * textFieldB in page B. After you jump to page B from page A, change the data in textFieldB and return to page A. labelA displays the changed data in textFieldB. Well, this is A simple data transfer scenario.

There are many solutions to this problem, such as using a DAO (data access object) to maintain the data corresponding to labelA and textFieldB. The Data Flow of the page is as follows:

Provides A proxy for page B to control access to page.To control page A, you can control labelA in page. However, the method for directly referencing an object above can also provide access to this object. Why must I use a proxy? Let's take a look at the UML diagram of the Agent Mode:

// DataTransDelegate @ protocol DataTransDelegate <NSObject>-(void) didTextFieldChanged :( UITextField *) textField; @ end

// ViewControllerA

// ViewControllerA. m # import "ViewControllerA. h "# import" ViewControllerB. h "# import" DataTransDelegate. h "@ interface ViewControllerA () <DataTransDelegate> @ property (strong, nonatomic) UILabel * labelA; @ property (strong, nonatomic) UIButton * buttonA; @ end @ implementation ViewControllerA-(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 (100,100,200, 50)]; _ labelA. text = @ "display textField content in vcB";} return _ labelA;}-(UIButton *) buttonA {if (_ buttonA = nil) {_ buttonA = [[UIButton alloc] initWithFrame: CGRectMake (100,200,100, 50)]; _ buttonA. backgroundColor = [UIColor blueColor]; [_ buttonA setTitle: @ "Enter vcB" forState: UIControlStateNormal];} return _ buttonA;} @ end

// ViewControllerB

// ViewControllerB. h @ protocol DataTransDelegate; @ interface ViewControllerB: UIViewController @ property (nonatomic, weak) id <DataTransDelegate> delegate; @ end // ViewController. m # import "ViewControllerB. h "# import" DataTransDelegate. h "@ interface ViewControllerB () <strong, DataTransDelegate> @ property (strong, nonatomic) UITextField * textFieldB; @ end @ implementation ViewControllerB-(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. delegate didTextFieldChanged: textField];}-(UITextField *) textFieldB {if (_ textFieldB = nil) {_ textFieldB = [[UITextField alloc] initWithFrame: CGRectMake (100,100,100, 50)]; _ textFieldB. text = @ "input text"; _ textFieldB. backgroundColor = [UIColor redColor];} return _ textFieldB;} @ end

The effect is as follows:

3. Misunderstanding of Agent Mode

In fact, there is no difference so far. The key is to use the protocol of Objective-C, which is generally used in combination with delegate. Most of us refer to this mode as the proxy mode, but I think the delegate mode is more like a delegate mode, rather than a real proxy, the proxy is the proxy, and the delegate is the delegate. In the proxy mode, both the proxy and the proxy must inherit from and implement the same interface Subject. To use delegate, you only need to let one of the classes inherit and implement the corresponding interfaces.

Delegation ModeIt is a basic technique in software design patterns. In the delegate mode, two objects are involved in processing the same request. The object receiving the request delegates the request to another object for processing. In fact, the above viewControllerB contains the reference of viewControllerA. This is the delegate mode.

For example, the most well-known UITableView is a typical delegation mode. It encapsulates unchanged parts of tableView and delegates frequently changed parts to the user for processing, therefore, UITableView is a delegator, and the class that follows UITableViewDelegate is delegate. Therefore, we often use self in a UIViewController. tableView. delegate = self;

You may wonder why you still need to use a UITableViewDelegate interface similar to the interface in Java? I personally understand that this facilitates the unification of interfaces and interfaces, which facilitates users, because you only need to implement these interfaces.

So we can see that the two blogs mentioned at the beginning actually implement the delegate mode by using the protocol in Objective-C.

If you have to talk about the relationship between the delegation mode and the proxy mode, I think the proxy mode should be a special delegation mode.

Related Article

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.