首页 > 代码库 > 观察者模式(猫大叫一声,主人惊醒,老鼠跑)
观察者模式(猫大叫一声,主人惊醒,老鼠跑)
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(); } }
观察者模式(猫大叫一声,主人惊醒,老鼠跑)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。