Pre-reading Knowledge reserve
Make sure that when you read this article, you have personally knocked over code about the proxy mode more than once. If not, please read after accumulating certain experience, believe will be more fruitful. Also hope that the great God feel free.
When do you use protocols and proxies?
Here is an example of the personal understanding of the agent, the agreement, hoping to play a valuable effect.
Suppose there is a task requirement: page A needs to jump to page B (some parameters may be passed in), page B fills in or handles some information when it jumps back to page A and needs to return the data A.
Do not use proxy mode in order to use proxy mode
Proxy mode is only a design pattern, its value lies in a unified model, to solve a originally inconvenient, or even almost impossible to solve the problem. That is, when the original, simple programming way can not deal with a problem, will think of using some design patterns (such as the agent model discussed in this article).
For example, if you say page a jumps to page B, you need to pass a parameter. It is obvious that the proxy pattern can be used to pass arguments. But is it necessary to use proxy mode at this time? My answer is--"never." Because you are jumping from page A to page B, the code should look like this:
//AViewController.m@property (strongnonatomic) BViewController *BVC;— (void)buttonDidClicked:(id)sender{ self.BVC = [[BViewController alloc]init]; [self.navigationController pushViewController:self.BVC animated:YES];}
Obviously the Viewcontroller of B is a property of the viewcontroller of a. Since a has a pointer to B, you can manipulate B directly with the pointer. You can assign a value to a property of B, you can call the method of B, and so on.
So when do I need to use proxy mode? My answer is: "Page b passes value to page a when". The answer is also very obvious, in page B, we do not even know that there is a page a, to page a value is even more impossible to talk about. A feasible scenario at this point is to define a protocol in page B and declare a proxy object. In page A, set yourself as the agent for Page B and complete the proxy method.
Thus, it is not difficult to draw a conclusion:
You can use proxy mode when an object cannot get a pointer to another object directly, and you want to do something about that variable.
What exactly does the proxy model do?
The proxy pattern in my eyes has only two concerns: protocols and proxies.
A protocol defines a set of methods that is implemented by a class.
As a property of a class, a surrogate is usually an instance object of another class, and can be responsible for accomplishing tasks that are inconvenient or impossible to accomplish in the original class.
First of all, talk about the agent, in the brain to recall the implementation of the proxy mode of the process. When defining a proxy object in page B, it seems to be very similar to defining a normal property (except for weak and ID, delegate >). This is my summary of the agent: The agent is a property, and not very mysterious.
Of course, the agent is not just a generic property, otherwise I just need to rewrite the initialization method of B to achieve the same effect:
self.BVC = [[BViewController alloc]initWithDelegate:self];
Then define a Aviewcontroller *AVC in BVIEWCONTROLLER.M and assign a value in the initialization method.
Notice that when the agent is defined, the format is often the same:
delegate;
So my understanding of the advantages of the agent is:
The core advantage of the agent is decoupling
As opposed to directly declaring a proxy that belongs to a fixed class, the agent declared as an ID has the advantage of two stars.
- Allow multiple different classes to become proxies for this class. Imagine if page B could jump back to n pages in this example, what if it was a way to declare a normal object?
- The class that allows the proxy is not yet fixed. Imagine, UITableView also has delegate, it doesn't know that the class will become its agent.
Take a look at the agreement. The protocol is much simpler. A protocol simply defines a set of methods. In proxy mode, it is entirely possible not to define a protocol in page B, and then a to follow this protocol. You can call the method of a directly.
Personally, the advantages of the agreement lie in the following points:
- You can take advantage of Xcode's check mechanism. For a method defined as @required, the compiler will have a warning if the protocol is implemented without implementing this method. This prevents you from inadvertently forgetting to implement a particular code, and because of the runtime characteristics of OC, such errors tend to cause the program to crash at run time.
- Facilitates the encapsulation of the code. If a class implements a protocol, the method in this protocol does not have to be declared in. h and can be called by the class that defines the protocol. This reduces the exposure of a class to external methods.
- It is advantageous to the structure and stratification of the program. A protocol is often a solution to a problem, and for a similar problem, we can only implement the protocol again, avoiding the idea of a set of methods. The inheritance mechanism of the protocol makes this a bit more powerful.
Say how much, summed up only one sentence: Proxy mode is not mysterious, but an optimized little trick (Let a class hold another class pointer). Proxies and protocols also only make the program less coupled and structurally more powerful.
Basics of iOS--on personal understanding of protocol and Agent