This is the ninth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

The official definition

Providing a proxy for other objects controls access to this object

For example, student A sends something to classmate B, but student A has something to do. At this time, student A needs to find another student C to help him deliver something. We say that C is A’s agent, and the delivery is an agreement between them

Components: 1, agreement: used to specify the agent to do what things (send things) 2, agent: according to the specified agreement, to complete the things prescribed by the agreement (C) 3, commission: according to the specified agreement, to specify the agent to complete the things (A)


Implementation of the concrete proxy pattern

  1. Defining a protocol
  • The name of a protocol usually begins with the class to which it belongs, followed by protocol or delegate (StudentAProtocol)

  • The legal name of the protocol usually begins with the name of the protocol protocol (student)

  • Normally, methods in a protocol will pass the object that triggered the protocol (studentA)

    #import <Foundation/Foundation.h>
    @class StudentA;
    @protocol StudentAProtocol <NSObject>
    - (void)studentADoSomething:(StudentA *)stu;
    @end
    Copy the code
  1. Define the delegate class and set up a proxy for it that adheres to the protocol

    #import <Foundation/Foundation.h>
    #import "StudentAProtocol.h"
    @interface StudentA : NSObject
    @property (nonatomic,weak) id<StudentAProtocol> delegate;
    @end
    Copy the code
  2. A method of defining a proxy class that implements a protocol

    #import <Foundation/Foundation.h>
    #import "StudentAProtocol.h"
    @interface StudentC : NSObject<StudentAProtocol>
    @end
    
    #import "StudentC.h"
    @implementation StudentC
    - (void)studentADoSomething:(StudentA *)stu {
        NSLog(@"%s",__func__);
    }
    @end
    
    Copy the code

4. Logical implementation

StudentA *stuA = [[StudentA alloc]init];
   StudentC *stuC = [[StudentC alloc]init];
   // The agent of student A is student C
   stuA.delegate = stuC;
   if([stuA.delegate respondsToSelector:@selector(studentADoSomething:)]) { [stuA.delegate studentADoSomething:stuA]; }}Copy the code

Principle of Proxy Usage

In essence, it is the transfer and operation of object memory. After the proxy object (C) is set by the delegate class (A), it is actually only A weak reference to the proxy object (C) with A pointer of type ID. The delegate class (A) lets the proxy (C) execute the agreement. It’s actually sending A message in the delegate class (student A) to the object that this pointer of type ID points to, which is the proxy object (student C).


Agent memory management

The keyword used to define properties is strong. When you set a proxy, strong creates a strong reference that affects the life cycle of an object and cannot be released

Weak assign, either of these keyword-modified pointer variables, does not change the reference count of the referenced object, but when an object is freed, weak automatically points to nil, while assign does not. In iOS, sending messages to nil does not crash, while assign causes an error of a wild pointer


The difference between agents and blocks

  • The same

    Can be thought of as a callback function that executes a sequence of events when something happens

  • The difference between

    • The delegate is used for heavy-duty callbacks, and the declaration and implementation of the method are separate, which doesn’t seem very coherent

    • Block has lightweight callbacks that allow direct access to the context, making the code structure more coherent

    • Blocks tend to create circular references; delegates do not