Design Pattern-Observer pattern (terrorist attack, Earthquake Example, class Relation Diagram)

Source: Internet
Author: User
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. text;
  4. Namespace consoleapplication1
  5. {
  6. Internal interface warning
  7. {
  8. String situation
  9. {
  10. Get;
  11. Set;
  12. }
  13. Void warn ();
  14. Void add (peopleinschool P );
  15. Void sub (peopleinschool P );
  16. }
  17. Class initialization estation: Warning
  18. {
  19. Private ilist <peopleinschool> Field = new list <peopleinschool> ();
  20. String situation;
  21. Public String situation
  22. {
  23. Get
  24. {
  25. Return situation;
  26. }
  27. Set
  28. {
  29. Situation = value;
  30. }
  31. }
  32. Public void warn ()
  33. {
  34. Foreach (peopleinschool P in field)
  35. {
  36. P. warningreceived ();
  37. }
  38. }
  39. Public void add (peopleinschool P)
  40. {
  41. Field. Add (P );
  42. }
  43. Public void sub (peopleinschool P)
  44. {
  45. Field. Remove (P );
  46. }
  47. }
  48. Class earthquakedepartment: Warning
  49. {
  50. String situation;
  51. Private ilist <peopleinschool> Field = new list <peopleinschool> ();
  52. Public String situation
  53. {
  54. Get
  55. {
  56. Return situation;
  57. }
  58. Set
  59. {
  60. Situation = value;
  61. }
  62. }
  63. Public void warn ()
  64. {
  65. Foreach (peopleinschool P in field)
  66. {
  67. P. warningreceived ();
  68. }
  69. }
  70. Public void add (peopleinschool P)
  71. {
  72. Field. Add (P );
  73. }
  74. Public void sub (peopleinschool P)
  75. {
  76. Field. Remove (P );
  77. }
  78. }
  79. Abstract class peopleinschool
  80. {
  81. Protected warning field;
  82. Public peopleinschool (warning W)
  83. {
  84. Field = W;
  85. }
  86. Public abstract void warningreceived ();
  87. }
  88. Class Teacher: peopleinschool
  89. {
  90. Public Teacher (warning W)
  91. : Base (W)
  92. {
  93. }
  94. Public override void warningreceived ()
  95. {
  96. Console. writeline ("I am a teacher" + field. Situation );
  97. }
  98. }
  99. Class student: peopleinschool
  100. {
  101. Public student (warning W)
  102. : Base (W)
  103. {
  104. }
  105. Public override void warningreceived ()
  106. {
  107. Console. writeline ("I am a student" + field. Situation );
  108. }
  109. }
  110. Class Client
  111. {
  112. Public static void main ()
  113. {
  114. Restore estation P = new restore estation ();
  115. Teacher T = new teacher (P );
  116. Student s = new student (P );
  117. P. Add (t );
  118. P. Add (s );
  119. P. Situation = "terrorist attack !!! ";
  120. P. Warn ();
  121. P. Situation = "Pay attention to fire prevention and theft !!! ";
  122. P. Warn ();
  123. Earthquakedepartment E = new earthquakedepartment ();
  124. T = new teacher (E );
  125. S = new student (E );
  126. E. Add (t );
  127. E. Add (s );
  128. E. Situation = "an earthquake is coming !!! ";
  129. E. Warn ();
  130. Console. Read ();
  131. }
  132. }
  133. }

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.