IOS---proxy design mode

Source: Internet
Author: User

The main content of the proxy design pattern today
    • What is the proxy design pattern (this is mainly the use of OC to implement proxy design mode)

      In short, the agent is to achieve message delivery, of course, at the same time we can also choose to notify, Kvo,block to achieve this process,
    • When to use proxy design mode

      Of course, when it is necessary to complete the transmission of information, it is used. So why not use proxies instead of using notifications or KVO? This is a more specific problem, you can refer to the following (agent, notification, KVO advantages and disadvantages)

      The advantages and disadvantages of agent, notice, KVO

How to write an agent design pattern
1. You need to be clear about the name of your agreement, generally the name is: Control class name + Delegate 2. The proxy method is generally declared as @optional (the program is @required by default) 3. The proxy method name typically starts with a control of 4. The proxy method has at least one parameter
So now let's give an example

This is the use of proxies to achieve the value of the blog

So, for example, how to use a proxy protocol between a nanny and a baby to achieve a simple process. Don't say anything, and then we're on the code. Before writing an example, we have a little need to clarify who the agent is.

Next we have no hesitation in the code,

First, we create one 婴儿类 , inherit from NSObject , and then create the following code in the. h file

#import <Foundation/Foundation.h> @class baby;//define an agent agreement @protocol babydelegate <nsobject>-(void) Babywanteat: (Baby *) baby;-(void) Babywantsleep: (Baby *) Baby, @end @interface baby:nsobject/** How many things to eat * * @property ( Nonatomic, assign) int food;/** drowsiness */@property (nonatomic, assign) int sleep;/** hungry */-(void) wanteat;/** trapped */-(void) Wan tsleep;/** Proxy Object */@property (nonatomic, strong) id<babydelegate> delegate; @end

Next, create the following file in the. m file

#import "Baby.h" @implementation baby-(void) wanteat{NSLog (@ "Baby wants to eat"); Inform the nanny to feed the baby [Self.delegate babywanteat:self];}    -(void) wantsleep{NSLog (@ "Baby wants to Sleep"); Inform the nanny to coax the baby to sleep [Self.delegate babywantsleep:self];} @end

The following creates a 保姆类 NSObject code in the. h file that likewise inherits from,

#import <Foundation/Foundation.h> @protocol babydelegate, @interface nurse:nsobject <BabyDelegate> @end

Next is the. m file in which the nanny needs to do something, this is still very necessary,

#import "Nurse.h" #import "Baby.h" @implementation nurse-(void) Babywanteat: (Baby *) baby{Baby.food + = 20; NSLog (@ "Nurse feeding the baby-now eats%d", baby.food);}    -(void) Babywantsleep: (Baby *) baby{baby.sleep + = 20; NSLog (@ "Nurse coax the baby to sleep--now the sleepiness is%d", baby.sleep);} @end

Here, a basic agent has been completed.


IOS---proxy design mode

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.