Design Mode-Protocol proxy Mode

Source: Internet
Author: User

In use cases, steps for use do not use protocol to implement proxy using protocol 1. In Use Case A wants B's help, B's proxy A wants to notify B that something has happened, or pass some data to B observer Mode 2. Use steps to define A Protocol B following the Protocol and implement the corresponding method A to define A proxy attribute: id <protocol> delegate creates B object, attaches its value to the delegate attribute of A. 3. If you do not use protocol to implement proxy, you do not use protocol to implement it. In this way, you can better compare it: what is the proxy. The classes used in the project are now designed for nurses to take care of their children. If the children are crying, eating, sleeping, and so on, the nurses immediately respond and take the action Baby. h. Copy the code /// Baby. h // 01-proxy mode /// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import <Cocoa/Cocoa. h> @ class Nurse; @ interface Baby: NSObject @ property (nonatomic, assign) int age; @ property (nonatomic, retain) Nurse * nurse;-(void) cry; -(void) eat;-(void) sleep; @ end copy the code Baby. m copy code /// Baby. m // 01-proxy mode //// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import "Baby. h "# import" Nurse. h "@ implementation Baby-(void) cry {NSLog (@" % d is crying ", _ age); [_ nurse cry: self];}-(void) eat {NSLog (@ "% d is hugry", _ age); [_ nurse eat: self] ;}- (void) sleep {NSLog (@ "% d is sleep", _ age); [_ nurse sleep: self] ;}@ end copy the code Nurse. h. Copy the code /// Nurse. h // 01-proxy mode /// Created by apple on 14-4-2. // Co Pyright (c) 2014 apple. all rights reserved. // # import <Foundation/Foundation. h> @ class Baby; @ interface Nurse: NSObject-(void) cry :( Baby *) bady;-(void) eat :( Baby *) bady;-(void) sleep :( Baby *) bady; @ end copy the code Nurse. m copy the code /// Nurse. m // 01-proxy mode /// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import "Nurse. h "@ implementation Nurse-(void) cry {NSLog (@" coax child ");}-(void) ea T {NSLog (@ "");}-(void) sleep {NSLog (@ "");} @ end copy the code main. m copy the code /// main. m // 01-proxy Mode 2 /// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import <Foundation/Foundation. h> # import "Baby. h "# import" Nurse. h "int main (int argc, const char * argv []) {@ autoreleasepool {Nurse * nurse = [[Nurse alloc] init]; baby * baby = [[Baby alloc] init]; baby. nurse = nurse; [baby Cry];} return 0;} it seems that there is no problem to copy the code, but if one day, the nurse suddenly does not care about the child, we give our children to a teacher to take care of them. Is there a lot to change? Even one day, the teacher did not take care of the child. We took care of the child's grandparents and needed to change a lot of content. So we can use protocol to implement 4. Use protocol to implement proxy 1.⃣Protocol we first define a protocol copy code // LookBaby. h // 01-proxy Mode 3 /// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import <Foundation/Foundation. h> @ class Baby; @ protocol LookBaby <NSObject>-(void) cry :( Baby *) baby;-(void) eat :( Baby *) baby;-(void) sleep :( Baby *) baby; @ end copy Code 2⃣️ Nurse follows the protocol and implements the methods in the Protocol. h. Copy the code /// Nurse. h // 01-proxy mode /// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import <Foundation/Foundation. h> # import "LookBaby. h "@ class Baby; @ interface Nurse: NSObject <LookBaby> @ end copy the code. m copy the code /// Nurse. m // 01-proxy mode /// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import "Nurse. h "@ implementation Nurse-(void) cry :( Baby *) baby {NSLog (@" coax child ") ;}- (void) eat :( Baby *) baby {NSLog (@ "");}-(void) sleep :( Baby *) baby {NSLog (@ "");} @ end copy code 3⃣Babybaby defines an implementation protocol attribute. h. Copy the code /// Baby. h // 01-proxy mode /// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import <Cocoa/Cocoa. h> # import "LookBaby. h "@ class Nurse; @ interface Baby: NSObject @ property (nonatomic, assign) int age; @ property (nonatomic, retain) id <LookBaby> loolBaby;-(void) cry; -(void) eat;-(void) sleep; @ end copy the code. m copy code /// Baby. m // 01-proxy mode /// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import "Baby. h "# import" Nurse. h "@ implementation Baby-(void) cry {NSLog (@" % d is crying ", _ age); [_ loolBaby cry: self];}-(void) eat {NSLog (@ "% d is hugry", _ age); [_ loolBaby eat: self] ;}- (void) sleep {NSLog (@ "% d is sleep", _ age); [_ loolBaby sleep: self] ;}@ end copy code 4⃣Pipeline creates object B and sets the delegate attribute of object A to copy the code of object B. // main. m // 01-proxy Mode 3 /// Created by apple on 14-4-2. // Copyright (c) 2014 apple. all rights reserved. // # import <Foundation/Foundation. h> # import "Baby. h "# import" Nurse. h "int main (int argc, const char * argv []) {@ autoreleasepool {Nurse * nurse = [[Nurse alloc] init]; baby * baby = [[Baby alloc] init]; baby. loolBaby = nurse; baby. age = 10; [baby eat];} return 0 ;}

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.