观察者模式

来源:互联网 发布:ug编程自学能学会吗 编辑:程序博客网 时间:2024/06/05 07:59

观察者模式又叫做 发布-订阅模式(Publish/Subscribe模式)


观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。

这个主题对象在状态发生变化的时, 会同时通知所有观察者对象,使他们能够自动更新自己。  

 

跟观察者一样,具体的通知者可能也有多个,他们也许有各自的一些方法。


观察者模式的目的

将一个系统分割成一系列相互协作的类有一个很不好的副作用,那就是需要维护相关对象的一致性。

我们不希望为了维持一致性而使各类紧密耦合,这样会给维护、扩展和重用都带来不便。

当一个对象的改变需要同时改变其他对象,且不知道具体有多少个对象有待改变时,应该考虑使用观察者模式。

观察者模式所做的工作其实就是在解除耦合。让耦合的双方都依赖于各自对应的抽象,而不是依赖于具体。

从而使得各自的变化都不会影响另一边变化。

①观察者类:

namespace ObserverMode{    abstract class GuanChaZhe    {        public string _name;        protected TongZhiZhe _tongzhizhe;        protected Form1 _form1;        //protected string _name;        //protected TongZhiZhe _tongzhizhe;        //protected Form1 _form1;        public GuanChaZhe( Form1 form1, string name, TongZhiZhe tongzhizhe)        {            this._form1 = form1;            this._name = name;            this._tongzhizhe = tongzhizhe;        }        //public void Updata()        //{        //    _form1.label4.Text = _tongzhizhe.Name + ": " + _tongzhizhe.Action;        //    _form1.label1.Text = this._name + ":关闭欧冠直播,继续工作!";        //}        public abstract void Updata();       }    //大师兄    class DaShiXiong : GuanChaZhe    {        public DaShiXiong( Form1 form1, string name, TongZhiZhe tongzhizhe)            : base(form1, name, tongzhizhe)        {        }        public override void Updata()        {            _form1.label4.Text = _tongzhizhe.Name + ": " + _tongzhizhe.Action;            _form1.label1.Text = this._name + ":关闭欧冠直播,继续工作!" ;        }    }    //城西    class ChenXi : GuanChaZhe    {        public ChenXi( Form1 form1, string name, TongZhiZhe tongzhizhe)            : base(form1, name, tongzhizhe)        {        }        public override void Updata()        {            _form1.label4.Text = _tongzhizhe.Name + ": " + _tongzhizhe.Action;            _form1.label2.Text = this._name + ":关闭某某美图,继续工作!" ;        }    }    //周游    class ZhouYou : GuanChaZhe    {        public ZhouYou( Form1 form1, string name, TongZhiZhe tongzhizhe)            : base(form1, name, tongzhizhe)        {        }        public override void Updata()        {            _form1.label4.Text = _tongzhizhe.Name + ": " + _tongzhizhe.Action;            _form1.label3.Text = this._name + ":关闭欧冠直播,继续工作!" ;        }    }}

②通知者类:

abstract class TongZhiZhe    {        protected List< GuanChaZhe> GuanChaZheMen = new List <GuanChaZhe >();        private string _name;        private string _action;        public string Name        {            get { return _name;}            set { _name = value; }        }        public string Action        {            get { return _action; }            set { _action = value; }        }        public TongZhiZhe( string name)        {            Name = name;        }        public abstract void Add( GuanChaZhe guanchazhe);        public abstract void Notify();    }    //小晴    class Xiaoqing : TongZhiZhe    {        public Xiaoqing( string name)            : base(name)        {        }        public override void Add( GuanChaZhe guanchazhe)        {            if (guanchazhe._name == "大师兄")                return;            GuanChaZheMen.Add(guanchazhe);               }        public override void Notify()        {            foreach ( GuanChaZhe item in GuanChaZheMen)            {                           item.Updata();            }        }    }

③客户端:

//通知者            TongZhiZhe xiaoqing = new Xiaoqing ("通知者" );            //观察者            GuanChaZhe dashixiong = new DaShiXiong (this , "大师兄" , xiaoqing);            GuanChaZhe chenxi = new ChenXi (this , "城西" , xiaoqing);            GuanChaZhe zhouyou = new ZhouYou (this , "周游" , xiaoqing);            //通知者记录            xiaoqing.Add(dashixiong);            xiaoqing.Add(chenxi);            xiaoqing.Add(zhouyou);            //tongzhizhe.Add(dashixiong);            //tongzhizhe.Add(chenxi);            //tongzhizhe.Add(zhouyou);            //发现老板回来了            xiaoqing.Action = "老板回来了!" ;            //通知者通知记录的观察者            xiaoqing.Notify();



0 0
原创粉丝点击