The article turns to: cainrun. Making. IO / 14718583940…


The author testimonials

Last time we saw how the singleton pattern works, now let’s explore what the proxy pattern is.

In fact, the so-called agency model in our daily life is very common, such as buying a car, have a variety of brands, but the auto makers and can’t pay for every corner in each city to open a own stores, so they will go to recruit some third-party agents for distribution, and this is what we say today the proxy pattern.

Finally: if you have better suggestions or dissatisfaction with this article, please contact me, I will refer to your opinions and then modify, contact me, please note the agent mode, if you feel good, I hope you can also reward ~ hee hee ~ WISH you a happy learning ~ thank you ~


Introduction to the

This is a UML diagram of the proxy pattern, and also a very simple explanation of what the proxy pattern is all about

The order is as follows:

  • 1. The Client initiates a request to the Subject to purchase a car
  • 2. The Subject will send the bill of lading to the Proxy
  • 3. The Proxy delivers the goods to the Subject according to the bill of lading
  • 4.Subject(代理点)再交付给Client(客户),

Create a project

Open the DesignPatternWork folder we created ourselves, which contains the DesignPatternWorkSpace we created earlier

Add the new project we want to create and name it ProxyPattern


Creating a proxy class

Declare proxy class

Let’s take Audi as a demonstration, forgive me for being an Audi fan


Declare proxy methods

Two proxy methods are declared here, but they differ in that one is mandatory and the other is optional. Since the method name is self-commented, I won’t add comments here.

@required
- (void)audiManufacturer:(AudiManufacturer *)audiManufacturer
           orderQuantity:(NSInteger)orderQuantity;Copy the code
@optional
- (void)audiManufacturer:(AudiManufacturer *)audiManufacturer
            audiDelegateInfo:(NSString *)audiDelegateInfo;Copy the code

Explain why we need to provide the agent class in the agent method. In fact, it is easy to understand. For example, take Audi as an example, cars of a brand must have different models, and each model may not be produced in the same factory. So in order to provide external differentiation, we need to provide proxy classes in proxy methods.


Implement the proxy method internally

Internally implement two methods that are open to each other

- (void)buyCarCount:(NSInteger)count;
- (void)buyCarModel:(NSString *)model;Copy the code


External invocation proxy

The proxy classes and proxy methods are done, and now we need to implement them externally


The difference between agency and agreement

Agency and Agreement

At this point, we have completed all the requirements for the entire proxy pattern, but there are a few minor details that need to be covered.

Protocol function: mainly used to constrain or filter objects. Proxy and protocol similarity: both proxy and protocol are declared using @protocol and provide a pattern of methods. The difference between a proxy and a protocol is that, as their function says, a proxy is used to decouple objects from each other, while a protocol is used to constrain or filter objects


Create a protocol

So let’s do this in code, just to make it a little bit easier, but notice that Protocol doesn’t have a dot m file


Create a customer class

After the Protocol is created, the customer class must comply with the Protocol we created, and then add a judgment to the Protocol.

Now let’s initialize the customer class and verify that our Protocol works.

First enter 10000

To enter 1000000


add

In fact, in iOS, there is a class called NSProxy, which can also be used to implement the proxy mode, but I will not introduce it here, later there is time to write another article to explain how to use NSProxy to implement the proxy mode, if you have a special curiosity friends, you can also go to check the information ~


The final summary

After the above demonstration, we know that the proxy mode is not as complex as imagined, to put it plainly, the so-called proxy mode is to reduce the coupling degree between the object and the object, in order to facilitate maintenance, at the same time we also know the difference between the proxy and the protocol, this article arrived, thank you for watching ~


The project address

Project address: github.com/CainRun/Des…