标签:ace hba att nat select protocol row ssi 设计
什么是设计模式
什么是代理设计模式
代理设计模式的场合:
// 协议 #import <Foundation/Foundation.h> @class Baby; @protocol BabyProtocol <NSObject> - (void)feedWithBaby:(Baby *)baby; - (void)hypnosisWithBaby:(Baby *)baby; @end
#import "BabyProtocol.h" @interface Baby : NSObject // 食量 @property (nonatomic, assign) int food; // 睡意 @property (nonatomic, assign) int drowsiness; // 饿 - (void)hungry; // 睡意 - (void)sleepy; @property (nonatomic, strong) id<BabyProtocol> nanny; @end @implementation Baby - (void)hungry { self.food -= 5; NSLog(@"婴儿饿了"); // 通知保姆 if ([self.nanny respondsToSelector:@selector(feedWithBaby:)]) { [self.nanny feedWithBaby:self]; } } - (void)sleepy { self.drowsiness += 5; NSLog(@"婴儿困了"); // 通知保姆 if ([self.nanny respondsToSelector:@selector(hypnosisWithBaby:)]) { [self.nanny hypnosisWithBaby:self]; } } @end
// 保姆 @interface Nanny : NSObject <BabyProtocol> @end @implementation Nanny - (void)feedWithBaby:(Baby *)baby { baby.food += 10; NSLog(@"给婴儿喂奶, 现在的食量是%i", baby.food); } - (void)hypnosisWithBaby:(Baby *)baby { baby.drowsiness += 10; NSLog(@"哄婴儿睡觉, 现在的睡意是%i", baby.drowsiness); } @end
标签:ace hba att nat select protocol row ssi 设计
原文地址:http://www.cnblogs.com/xufengyuan/p/6613658.html