首页 > 代码库 > 观察者模式(猫大叫一声,主人惊醒,老鼠跑)

观察者模式(猫大叫一声,主人惊醒,老鼠跑)

    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();        }    }

 

观察者模式(猫大叫一声,主人惊醒,老鼠跑)