
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace ConsoleApplication1
- {
- internal interface Warning
- {
- String Situation
- {
- get;
- set;
- }
- void Warn();
- void Add(PeopleInSchool p);
- void Sub(PeopleInSchool p);
- }
- class PoliceStation : Warning
- {
- private IList<PeopleInSchool> field = new List<PeopleInSchool>();
- string situation;
- public string Situation
- {
- get
- {
- return situation;
- }
- set
- {
- situation = value;
- }
- }
- public void Warn()
- {
- foreach (PeopleInSchool p in field)
- {
- p.WarningReceived();
- }
- }
- public void Add(PeopleInSchool p)
- {
- field.Add(p);
- }
- public void Sub(PeopleInSchool p)
- {
- field.Remove(p);
- }
- }
- class EarthquakeDepartment : Warning
- {
- string situation;
- private IList<PeopleInSchool> field = new List<PeopleInSchool>();
- public string Situation
- {
- get
- {
- return situation;
- }
- set
- {
- situation = value;
- }
- }
- public void Warn()
- {
- foreach (PeopleInSchool p in field)
- {
- p.WarningReceived();
- }
- }
- public void Add(PeopleInSchool p)
- {
- field.Add(p);
- }
- public void Sub(PeopleInSchool p)
- {
- field.Remove(p);
- }
- }
- abstract class PeopleInSchool
- {
- protected Warning field;
- public PeopleInSchool(Warning w)
- {
- field = w;
- }
- public abstract void WarningReceived();
- }
- class Teacher : PeopleInSchool
- {
- public Teacher(Warning w)
- : base(w)
- {
- }
- public override void WarningReceived()
- {
- Console.WriteLine("我是老师" + field.Situation);
- }
- }
- class Student : PeopleInSchool
- {
- public Student(Warning w)
- : base(w)
- {
- }
- public override void WarningReceived()
- {
- Console.WriteLine("我是学生" + field.Situation);
- }
- }
- class Client
- {
- public static void Main()
- {
- PoliceStation p = new PoliceStation();
- Teacher t = new Teacher(p);
- Student s = new Student(p);
- p.Add(t);
- p.Add(s);
- p.Situation = "有恐怖袭击!!!";
- p.Warn();
- p.Situation = "注意防火防盗!!!";
- p.Warn();
- EarthquakeDepartment e = new EarthquakeDepartment();
- t = new Teacher(e);
- s = new Student(e);
- e.Add(t);
- e.Add(s);
- e.Situation = "要地震了!!!";
- e.Warn();
- Console.Read();
- }
- }
- }