[Oc learning diary] proxy mode and oc diary proxy Mode

Source: Internet
Author: User

[Oc learning diary] proxy mode and oc diary proxy Mode

What is the proxy mode? The proxy mode is also the delegate mode.

The proxy mode, as its name implies, is a person asking another person to help him do something.

Next we will use a purchase story to explain the agent mode.

1. First, we need to create a protocol with specific functions (purchasing functions)

1 # import <Foundation/Foundation. h> 2 // declaration protocol 3 @ protocol delegate <NSObject> 4 // method of purchasing in the protocol 5-(void) buy; 6 @ end

2. Who can implement this function will let this class follow this agreement (in this example, the purchaser can implement the purchasing function, so the purchase header file follows this Agreement)

1 # import <Foundation/Foundation. h> 2 # import "delegate. h "3 # import" WantBuy. h "4 // follow protocol 5 @ interface HelpBuy: NSObject <delegate> 6 // Method for receiving requests 7-(void) reservebuy; 8 @ end

And implement the methods in the Protocol

1 # import "HelpBuy. h "2 3 @ implementation HelpBuy 4 // the purchaser can implement the purchasing function 5-(void) buy 6 {7 NSLog (@" I am purchasing, I can buy "); 8} 9 // The purchaser receives the 10-(void) reservebuy11 {12 WantBuy * wanter = [[WantBuy alloc] init]; 13 wanter. mydel = self; 14 // request to the purchaser 15 [wanter dementbuy]; 16} 17 @ end

3. Sleep needs to use this function (who needs this function, in this example, wants the purchaser to need the purchaser to help him buy the product, so he wants to define an agreement attribute pointing to the purchaser)

1 # import <Foundation/Foundation. h> 2 # import "delegate. h "3 @ interface WantBuy: NSObject4 // you need to define a protocol attribute for the purchaser. 5 @ property (nonatomic, assign) id <delegate> mydel; 6-(void) dementbuy; 7 @ end

If you want the purchaser to send a request to the purchaser

1 # import "WantBuy. h "2 3 @ implementation WantBuy 4-(void) dementbuy 5 {6 NSLog (@" I want to buy you for me "); 7 // send the purchase request 8 [_ mydel buy]; 9} 10 @ end

 

 

In general, the proxy mode can be roughly divided into two implementation methods.

1. Who makes what?

2. Who asked me to do what?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.