public delegate void SubEventHandle(); public abstract class Subject { public event SubEventHandle SubEvent; public void OnSub() { if (SubEvent != null) SubEvent(); } } public abstract class Observer { public Observer(Subject sub) { sub.SubEvent += new SubEventHandle(Response); } protected abstract void Response(); } public class Mouse : Observer { public Mouse(Subject sub):base(sub) { } public override void Response() { } } public class Master : Observer { public Master(Subject sub) : base(sub) { } protected override void Response() { } } public class Cat : Subject { private List<Observer> observers; public Cat() { observers = new List<Observer>(); } public void Cry() { base.OnSub(); } }
时间: 2024-10-05 09:25:42