The role of proxy design patterns :
1.A Object Listener B Some of the behavior of the object, A become B the agent
2.B object wants to tell A object Some things, A become B the agent
Summary of proxy design patterns:
If you want to listen to other people's behavior, then you need to be someone else's agent.
If you want to tell someone something, then let someone else be your agent.
Development steps for agent design patterns
1. Prepare an agreement (the format of the agreement name: control name + Delegate) and declare some proxy methods in the protocol (the general proxy method is @optional )
2. Declare a proxy property:@property (nonatomic, weak) id< Agent Agreement > delegate;
3. When certain behavior occurs internally, call the proxy method of the agent to inform the agent what's going on inside
4. Set proxy:xxx.delegate = yyy;
5.yyy Object Compliance Protocol, implementing proxy method
Sample program:
The program structure is as follows, the Controller will act as the agent of the footer class, and implement the method in the proxy class, in the footer class, judge whether the controller implements the method in the proxy class, if any, it will call the agent method in the controller.
1. The diagram of the controller and the footer class is as follows:
2. In the header file of the footer class,
Declare a good lyloadmorefooterdelegate proxy, can be considered to be a provider of methods of interface,<nsobject> represents the support of any object, @optional that the following method is implemented as non-mandatory. Then declare a proxy object delegate, which requires an object that implements the <LYLoadMoreFooterDelegate> protocol (interface), which is the following controller object, Whoever is assigned to this object must implement the proxy or interface method.
3. In the. m file of the footer class,
Where the proxy method needs to invoke the proxy class, first determine whether the proxy class actually implements the proxy method that needs to be invoked, if there is an implementation.
4. In the Controller. m file,
The first step is to implement the Lyloadmorefooterdelegate (proxy/interface).
Then, in the first load method, instantiate the footer class, and assign a value of self to the delegate object of the footer class, representing itself as the proxy for the footer object, the following method is the method to be implemented after becoming an agent.
ios-Proxy Mode