设计模式---观察者模式(恐怖袭击、地震的例子,有类关系图)

设计模式---观察者模式(恐怖袭击、地震的例子,有类关系图)
 
  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 PoliceStation : 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("我是老师" + 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("我是学生" + field.Situation);
  108.         }
  109.     }
  110.     class Client
  111.     {
  112.         public static void Main()
  113.         {
  114.             PoliceStation p = new PoliceStation();
  115.             Teacher t = new Teacher(p);
  116.             Student s = new Student(p);
  117.             p.Add(t);
  118.             p.Add(s);
  119.             p.Situation = "有恐怖袭击!!!";
  120.             p.Warn();
  121.             p.Situation = "注意防火防盗!!!";
  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 = "要地震了!!!";
  129.             e.Warn();
  130.             Console.Read();
  131.         }
  132.     }
  133. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值