码迷,mamicode.com
首页 > 其他好文 > 详细

笔记-大话设计模式-14 观察者模式

时间:2015-09-08 18:20:02      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

观察者模式又叫做发布-订阅(Publish/Subscribe)模式。
观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己。

观察者模式所做的工作其实就是解除耦合。让耦合的双方都依赖于抽象,而不是依赖于具体。从而使得各自的变化都不会影响另一边的变化。

Demo1:

    interface Subject
    {
        void Attach(Observer observer);
        void Detach(Observer observer);
        void Notify();
        string SubjectState
        {
            set;
            get;
        }
    }
    abstract class Observer
    {
        protected string name;
        protected Subject subject;

        public Observer(string name, Subject subject)
        {
            this.name = name;
            this.subject = subject;
        }

        public abstract void Update();
    }
    class StockObserver : Observer
    {
        public StockObserver(string name, Subject subject)
            : base(name, subject)
        {

        }

        public override void Update()
        {
            Console.WriteLine("{0} {1} 关闭股票行情,继续工作!", subject.SubjectState, name);
        }
    }
    class Boss : Subject
    {
        private IList<Observer> observers = new List<Observer>();
        private string action;

        public void Attach(Observer observer)
        {
            observers.Add(observer);
        }

        public void Detach(Observer observer)
        {
            observers.Remove(observer);
        }

        public void Notify()
        {
            foreach (var item in observers)
            {
                item.Update();
            }
        }

        public string SubjectState
        {
            get
            {
                return action;
            }
            set
            {
                action = value;
            }
        }
    }

Test:

            Subject boss = new Boss();
            Observer p1 = new StockObserver("Jack", boss);
            boss.Attach(p1);
            boss.SubjectState = "老板回来了";
            boss.Notify();

Demo2(improvement version):

    interface Subject2
    {
        //void Attach(Observer observer);
        //void Detach(Observer observer);
        void Notify();
        string SubjectState
        {
            set;
            get;
        }
    }
    class Boss2 : Subject2
    {
        //private IList<Observer> observers = new List<Observer>();
        private string action;
        public event Action Update;
        //public void Attach(Observer observer)
        //{
        //    observers.Add(observer);
        //}

        //public void Detach(Observer observer)
        //{
        //    observers.Remove(observer);
        //}

        public void Notify()
        {
            //foreach (var item in observers)
            //{
            //    item.Update();
            //}
            Update();
        }

        public string SubjectState
        {
            get
            {
                return action;
            }
            set
            {
                action = value;
            }
        }
    }
    class CoderObserver
    {
        private string name;
        private Subject2 subject;
        public CoderObserver(string name, Subject2 subject)
        {
            this.name=name;
            this.subject=subject;
        }

        public void CoderObserverUpdate()
        {
            Console.WriteLine("{0} {1} 程序员,继续写代码!", subject.SubjectState, name);
        }
    }
    class StockObserver2
    {
        private string name;
        private Subject2 subject;
        public StockObserver2(string name, Subject2 subject)
        {
            this.name=name;
            this.subject=subject;
        }

        public void StockObserverUpdate()
        {
            Console.WriteLine("{0} {1} 关闭股票行情,继续工作!", subject.SubjectState, name);
        }
    }

Test:

            Boss2 boss = new Boss2();
            StockObserver2 ob1 = new StockObserver2("Jack", boss);
            CoderObserver co = new CoderObserver("Lei", boss);
            boss.Update += ob1.StockObserverUpdate;
            boss.Update += co.CoderObserverUpdate;
            boss.SubjectState = "老板回来了";
            boss.Notify();

 

笔记-大话设计模式-14 观察者模式

标签:

原文地址:http://www.cnblogs.com/laixiancai/p/4792301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!