Personally, I prefer to convert the delegate mode into the proxy mode. In other words, the first contact with the proxy mode is in Java, so it is essential to implement the modern rational mode and interface in Java. At that time, when I learned spring, I used the interface callback, which is actually the delegated proxy mode. Simply put, the proxy is to hand over the corresponding functions to the corresponding class implementing the interface. How can I implement proxy when there is no interface in oc? In the previous blog, I mentioned that the protocols in OC are very similar to interfaces in Java, and they are implemented only by declaring methods, the implementation of the method submits a class that complies with the Protocol in OC, and the implementation of the method in Java is handed over to the class that implements the interface. In fact, it is better to understand a new language than the language you have learned before. Let's talk about the delegated proxy mode in OC.
The following uses the example of housing agencies and landlords to represent the Agency mode in OC. A charter company entrusts a house to a rental agency for rent. It is okay for a charter company to collect the rent every month ~.
1. if I write proxy in Java, I will first write the interface of the House intermediary, so in OC, we must first declare the house intermediary protocol, the method in protocol is a required method in every housing intermediary, so it will be abstracted as an interface in Java, and we will declare it as a protocol in OC, then, declare each method as required. The Protocol is as follows:
12345678910111213141516 |
// // SaleHouse.h // Memory // // Created by ludashi on 14-8-7. // Copyright (c) 2014 Mr. Li. All rights reserved. // #import <Foundation/Foundation.h> @protocol SaleHouse <NSObject> // Define two required methods in the House selling agreement @required // The intermediary sells the house - ( void )saleHouse; // Send the money to the user - ( void )payMoney; @end |
2. We don't have to worry about declaring an intermediary because there are too many intermediaries! The customer is the god of the house to be rented. So we should first change the public class to the public class. The public class is not highly demanding for intermediary companies, as long as we can rent out the house for me, then the money will be paid, so we only need to abide by the agreement of the previously defined Housing Agency. We can define the Charter public as follows:
The interface code is as follows:
123456789101112131415161718192021 |
// Landlord.h // Memory // // Created by Master Lu on 14-8-7. // Copyright (c) 2014 Mr. Li. All rights reserved. // #import <Foundation/Foundation.h> // Introduce the Protocol #import "SaleHouse.h" @interface Landlord : NSObject // The landlord's intermediary company can purchase and pay for the house. // The agent is a protocol-compliant object. @property (assign) id<SaleHouse> agent; // Rent out a house and entrust it to the Agency -( void ) saleHouse; // Rent the package -( void ) receiveMoney; @end |
Implementation Code: In the implementation code, the Charter Guild entrusts the house to the rental intermediary through the salehouse method in the Agreement
12345678910111213141516171819 |
#import "Landlord.h" @implementation Landlord // Rent out a house and entrust it to the Agency -( void ) saleHouse { NSLog(@ "I'm a charter company: ludashi. I rented out the house! " ); // Call the entrusting company's house selling method and the entrusting relationship call [self.agent saleHouse]; } // Rent the package -( void ) receiveMoney { NSLog(@ "Ludashi: I am the most happy to rent a house every month, because I can rent a house again! " ); } @end |
3. after the public rental is completed, we will start to look for an intermediary company. The intermediary company must first comply with our protocol (in Java, we need to implement our previous interface ), in an intermediary company, we use a shared-income class because we want to establish a partnership with a Public Charter. We use @ class to declare it, so as to reduce coupling between modules.
The interface code is as follows:
123456789 |
#import <Foundation/Foundation.h> // Introduce the Protocol File #import"SaleHouse.h" // In order to decouple the landlord class, we use @ class to declare the landlord class, instead of introducing the header file of the landlord. @ class Landlord; @interface HouseSaler : NSObject<SaleHouse> // The landlord is an intermediary customer, so define a customer object @property (assign) Landlord *customer; @end |
Implementation Method:
123456789101112131415161718 |
#import "HouseSaler.h" #import "Landlord.h" @implementation HouseSaler // Implement the house selling method -( void )saleHouse { NSLog(@ "Agent: I rented a house. I have many houses ;" ); [self payMoney]; } // Pay the user -( void ) payMoney { NSLog(@ "Agent: house rental now! Ah! You have to pay the user !! " ); [self.customer receiveMoney]; } @end |
4. Test: renting a public account is looking for an intermediary company
123456789101112 |
// Test the delegation Mode // Instantiate the public package Landlord *ludashi = [Landlord new ]; // Instantiate the intermediary HouseSaler *agent = [HouseSaler new ]; // The user selects an agent company. This company only needs to comply with the lease agreement. ludashi.agent = agent; agent.customer = ludashi; // The landlord buys a house [ludashi saleHouse]; |
Running result:
1234 |
18:04:01. 717 memory [21273: 303] I am a charter company: ludashi. I rented out the house! 18:04:01. 719 memory [21273: 303] AGENT: rented a house. I have many houses; 18:04:01. 719 memory [21273: 303] AGENT: rent out the house! Ah! You have to pay the user !! 18:04:01. 720 memory [21273: 303] ludashi! |