IOS _ proxy design demo
Main. m
//// Main. m // proxy design mode/* Three Use Cases of proxy design mode 1. A wants B to do something for him, and B can become A proxy for A. 2, A wants to notify B and A that something has happened, so that B can be the proxy of A. 3. B wants to listen to something that has happened to, let B become the proxy of A first proxy set mode standard 4 step 1, define A protocol 2, B wants to act as A proxy, must first comply with and implement the above protocol 3, define A member id in
Delegate 4: Assign the Instance Object of B to the member variable delegate * // Created by beyond on 14-7-25. // Copyright (c) 2014 com. beyond. all rights reserved. // # import
# Import "Baby. h "# import" nana. h "int main (int argc, const char * argv []) {@ autoreleasepool {Baby * bb = [[Baby alloc] init]; bb. age = 1; bb. nanny = [[nana alloc] init]; // [bb hungry];} return 0 ;}
Protocol
BabyNannyDelegate. h
/// BabyNannyDelegate. h // proxy Setting Mode /// Created by beyond on 14-7-25. // Copyright (c) 2014 com. beyond. all rights reserved. // # import
@ Class Baby; @ protocol BabyNannyDelegate
// Three methods stipulated in the babysitter agreement // (void) feedBaby (Baby *) baby feeding; // amused Baby-(void) amuseBaby (Baby *) baby; // pat the Baby to make it fall asleep-(void) patBaby :( baby *) baby; @ end
Baby. h
//// Baby. h // proxy Setting Mode /// Created by beyond on 14-7-25. // Copyright (c) 2014 com. beyond. all rights reserved. // # import
# Import "BabyNannyDelegate. h "@ interface Baby: NSObject // if the object is strong id, it indicates that any object can be used as a nanny, as long as it complies with the nanny agreement and implements three methods in the Protocol @ property (nonatomic, strong) id
Nanny; @ property (nonatomic, assign) int age;-(void) cry; // baby crying-(void) sleep; // baby wants to sleep-(void) hungry; // The baby wants to eat milk @ end
Baby. m
//// Baby. m // proxy Setting Mode /// Created by beyond on 14-7-25. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "Baby. h "@ implementation Baby-(void) cry {NSLog (@" % d years old Baby... cry ", _ age); // call the method of happy baby in the proxy [_ Nanny amuseBaby: self];}-(void) sleep {NSLog (@ "% d years old baby... want to go to bed ", _ age); // call the method in the proxy to beat the baby to make it fall asleep [_ Nanny patBaby: self];}-(void) hungry {NSLog (@ "% d years old baby... want to eat milk ", _ age); // call the method in the proxy for feeding the baby [_ Nanny feedBaby: self];} @ end
Nanny nana. h
//// Nana. h // proxy Setting Mode /// Created by beyond on 14-7-25. // Copyright (c) 2014 com. beyond. all rights reserved. // # import
# Import "BabyNannyDelegate. h" // if nana wants to take care of a baby now, she must abide by the babysitter agreement and implement the @ interface nana: NSObject
@ End
Nanny nana. m
//// Nana. m // proxy Setting Mode /// Created by beyond on 14-7-25. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "nana. h "# import" Baby. h "@ implementation nana-(void) feedBaby :( Baby *) baby {NSLog (@" nana is breastfeeding % d ", baby. age);}-(void) amuseBaby :( Baby *) baby {NSLog (@ "Nana is yelling at % d-year-old baby happy", Baby. age) ;}- (void) patBaby :( Baby *) baby {NSLog (@ "Nana is gently tapping % d-year-old baby to fall asleep", Baby. age) ;}@ end