Objective-C: 11_proxy Design Mode

Source: Internet
Author: User

Proxy design mode:

For example, if you want to watch a movie, you need to buy a ticket ). In this way, we can find a proxy to buy tickets. 1. First define a Protocol @ protocol ticketdelegate <nsobject>
// Return the fare-(double) ticketprice; // The number of remaining tickets-(INT) leftticketsnumber;
@ End 2. The proxy complies with this Protocol @ interface agent: nsobject <ticketdelegate> @ end 3. In the person class, declare @ property to comply with the Protocol variables, then declare a method to buy a movie ticket @ interface person: nsobject
@ Property (nonatomic, strong) ID <ticketdelegate>Delegate; // Do not enter the proxy class here. The ID is used.// Buy movie tickets-(void) buyticket; @ end
4. Implementation of the person class @ implementation person
-(Void) buyticket {double price = [_ delegate ticketprice]; int number = [_ delegate leftticketsnumber]; nslog (@ "via proxy help, fare = % F, % d tickets remaining ", price, number) ;}@ end
Proxy design mode: to complete a task and define a protocol, as long as the class complies with this Protocol, it can be done. When a proxy is required, we use the universal pointer ID when declaring it, you do not need to determine which class to complete. You only need to assign the object of the class complying with the Protocol to the declared member variable.

Objective-C: 11_proxy Design Mode

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.