- iOS程序源代码下载链接:03-代理设计模式.zip
28.3 KB // main.m
- //
- // main.m
- // 03-代理设计模式
- //
- // Created by apple on 13-11-27.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<Foundation/Foundation.h>
- #import"Baby.h"
- //#import "Student.h"
- #import"Dog.h"
- int main(int argc,const char* argv[])
- {
- @autoreleasepool{
- // 1.学生
- // Nurse *nurse = [[Nurse alloc] init];
- // Teacher *teacher = [[Teacher alloc] init];
- // Student *stu = [[Student alloc] init];
- // 2.婴儿
- Baby *b = [[Baby alloc] init];
- b.age =1;
- // b.baomu = stu;
- b.baomu = [[Dog alloc] init];
- [b sleep];
- }
- return 0;
- }
// Baby.h
- //
- // Baby.h
- // 03-代理设计模式
- //
- // Created by apple on 13-11-27.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<Foundation/Foundation.h>
- #import"BabyBaomuXieyi.h"
- @interface Baby : NSObject
- @property(nonatomic,strong) id<BabyBaomuXieyi> baomu;//任何人都能做保姆
- @property(nonatomic,assign)int age;//年龄
- - (void)hungry;//饿了
- - (void)cry;//哭了
- - (void)sleep;//想睡觉
- @end
// Baby.m
- //
- // Baby.m
- // 03-代理设计模式
- //
- // Created by apple on 13-11-27.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"Baby.h"
- @implementation Baby
- - (void)cry
- {
- NSLog(@"%d岁的baby哭了", _age);
- //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3493396.html
- //通知保姆(护士)
- [_baomu babyCry:self];
- }
- - (void)sleep
- {
- NSLog(@"%d岁的baby想睡觉", _age);
- //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3493396.html
- //通知保姆
- [_baomu babySleep:self];
- }
- - (void)hungry
- {
- NSLog(@"%d岁的baby想吃东西", _age);
- //通知保姆
- [_baomu babyHungry:self];
- }
- @end
// Dog.h
- //
- // Dog.h
- // 03-代理设计模式
- //
- // Created by apple on 13-11-27.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<Foundation/Foundation.h>
- #import"BabyBaomuXieyi.h"
- @interface Dog : NSObject <BabyBaomuXieyi>
- @end
// Dog.m
- //
- // Dog.m
- // 03-代理设计模式
- //
- // Created by apple on 13-11-27.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"Dog.h"
- #import"Baby.h"
- @implementation Dog
- - (void)babyCry:(Baby *)baby
- {
- NSLog(@"狗正在安慰%d岁的baby", baby.age);
- }
- - (void)babyHungry:(Baby *)baby
- {
- NSLog(@"狗喂%d岁的baby吃东西", baby.age);
- }
- - (void)babySleep:(Baby *)baby
- {
- NSLog(@"狗哄%d岁的baby去睡觉", baby.age);
- }
- @end
// Nurse.h
- //
- // Nurse.h
- // 03-代理设计模式
- //
- // Created by apple on 13-11-27.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<Foundation/Foundation.h>
- @class Baby;
- @interface Nurse : NSObject
- - (void)babyCry:(Baby *)baby;
- - (void)babySleep:(Baby *)baby;
- - (void)babyHungry:(Baby *)baby;
- @end
// Nurse.m
- //
- // Nurse.m
- // 03-代理设计模式
- //
- // Created by apple on 13-11-27.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"Nurse.h"
- #import"Baby.h"
- @implementation Nurse
- - (void)babyCry:(Baby *)baby
- {
- NSLog(@"护士正在安慰%d岁的baby", baby.age);
- }
- - (void)babyHungry:(Baby *)baby
- {
- NSLog(@"护士喂%d岁的baby吃东西", baby.age);
- }
- - (void)babySleep:(Baby *)baby
- {
- NSLog(@"护士哄%d岁的baby去睡觉", baby.age);
- }
- @end