代理设计模式iOS开发Demo(示例程序)源代码

时间:2024-01-02 17:01:41

 

 

  1. iOS程序源代码下载链接:代理设计模式iOS开发Demo(示例程序)源代码03-代理设计模式.zip
    28.3 KB
  2. // main.m

  3. //
  4. //  main.m
  5. //  03-代理设计模式
  6. //
  7. //  Created by apple on 13-11-27.
  8. //  Copyright (c) 2013年itcast. All rights reserved.
  9. //
  10. #import<Foundation/Foundation.h>
  11. #import"Baby.h"
  12. //#import "Student.h"
  13. #import"Dog.h"
  14. int main(int argc,const char* argv[])
  15. {
  16.    @autoreleasepool{
  17.        // 1.学生
  18. //        Nurse *nurse = [[Nurse alloc] init];
  19. //        Teacher *teacher = [[Teacher alloc] init];
  20. //        Student *stu = [[Student alloc] init];
  21.        // 2.婴儿
  22.         Baby *b = [[Baby alloc] init];
  23.         b.age =1;
  24. //        b.baomu = stu;
  25.         b.baomu = [[Dog alloc] init];
  26.          [b sleep];
  27.     }
  28.    return 0;
  29. }
  30. // Baby.h

  31. //
  32. //  Baby.h
  33. //  03-代理设计模式
  34. //
  35. //  Created by apple on 13-11-27.
  36. //  Copyright (c) 2013年itcast. All rights reserved.
  37. //
  38. #import<Foundation/Foundation.h>
  39. #import"BabyBaomuXieyi.h"
  40. @interface Baby : NSObject
  41. @property(nonatomic,strong) id<BabyBaomuXieyi> baomu;//任何人都能做保姆
  42. @property(nonatomic,assign)int age;//年龄
  43. - (void)hungry;//饿了
  44. - (void)cry;//哭了
  45. - (void)sleep;//想睡觉
  46. @end
  47. // Baby.m

  48. //
  49. //  Baby.m
  50. //  03-代理设计模式
  51. //
  52. //  Created by apple on 13-11-27.
  53. //  Copyright (c) 2013年itcast. All rights reserved.
  54. //
  55. #import"Baby.h"
  56. @implementation Baby
  57. - (void)cry
  58. {
  59.     NSLog(@"%d岁的baby哭了", _age);
  60.    //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3493396.html
  61.    //通知保姆(护士)
  62.     [_baomu babyCry:self];
  63. }
  64. - (void)sleep
  65. {
  66.     NSLog(@"%d岁的baby想睡觉", _age);
  67.   //转载请注明出处--本文永久链接:http://www.cnblogs.com/ChenYilong/p/3493396.html 
  68.    //通知保姆
  69.     [_baomu babySleep:self];
  70. }
  71. - (void)hungry
  72. {
  73.     NSLog(@"%d岁的baby想吃东西", _age);
  74.    
  75.    //通知保姆
  76.     [_baomu babyHungry:self];
  77. }
  78. @end
  79. // Dog.h

  80. //
  81. //  Dog.h
  82. //  03-代理设计模式
  83. //
  84. //  Created by apple on 13-11-27.
  85. //  Copyright (c) 2013年itcast. All rights reserved.
  86. //
  87. #import<Foundation/Foundation.h>
  88. #import"BabyBaomuXieyi.h"
  89. @interface Dog : NSObject <BabyBaomuXieyi>
  90. @end
  91. // Dog.m

  92. //
  93. //  Dog.m
  94. //  03-代理设计模式
  95. //
  96. //  Created by apple on 13-11-27.
  97. //  Copyright (c) 2013年itcast. All rights reserved.
  98. //
  99. #import"Dog.h"
  100. #import"Baby.h"
  101. @implementation Dog
  102. - (void)babyCry:(Baby *)baby
  103. {
  104.     NSLog(@"狗正在安慰%d岁的baby", baby.age);
  105. }
  106. - (void)babyHungry:(Baby *)baby
  107. {
  108.     NSLog(@"狗喂%d岁的baby吃东西", baby.age);
  109. }
  110. - (void)babySleep:(Baby *)baby
  111. {
  112.     NSLog(@"狗哄%d岁的baby去睡觉", baby.age);
  113. }
  114. @end
  115. // Nurse.h

  116. //
  117. //  Nurse.h
  118. //  03-代理设计模式
  119. //
  120. //  Created by apple on 13-11-27.
  121. //  Copyright (c) 2013年itcast. All rights reserved.
  122. //
  123. #import<Foundation/Foundation.h>
  124. @class Baby;
  125. @interface Nurse : NSObject
  126. - (void)babyCry:(Baby *)baby;
  127. - (void)babySleep:(Baby *)baby;
  128. - (void)babyHungry:(Baby *)baby;
  129. @end
  130. // Nurse.m

  131. //
  132. //  Nurse.m
  133. //  03-代理设计模式
  134. //
  135. //  Created by apple on 13-11-27.
  136. //  Copyright (c) 2013年itcast. All rights reserved.
  137. //
  138. #import"Nurse.h"
  139. #import"Baby.h"
  140. @implementation Nurse
  141. - (void)babyCry:(Baby *)baby
  142. {
  143.     NSLog(@"护士正在安慰%d岁的baby", baby.age);
  144. }
  145. - (void)babyHungry:(Baby *)baby
  146. {
  147.     NSLog(@"护士喂%d岁的baby吃东西", baby.age);
  148. }
  149. - (void)babySleep:(Baby *)baby
  150. {
  151.     NSLog(@"护士哄%d岁的baby去睡觉", baby.age);
  152. }
  153. @end

 

https://www.evernote.com/shard/s227/sh/ad26c268-cd1e-463a-a093-bbe84802abf8/f452d3af63abdc7066cab8571c3e9c50