《大话设计模式》学习笔记10:观察者模式

  《大话设计模式》学习笔记10:观察者模式_第1张图片

  《大话设计模式》学习笔记10:观察者模式_第2张图片

观察老板回来示例:

  《大话设计模式》学习笔记10:观察者模式_第3张图片

1.Subject:

    public interface Subject
    {
        void Attach(Observer observer);
        void Detach(Observer observer);
        void Notify();
        string SubjectState { get; set; }
    }

2.ConcreteSubject(以Boss为例):

    public class Boss:Subject
    {
        private IList<Observer> observers = new List<Observer>();
        public void Attach(Observer observer)
        {
            observers.Add(observer);
        }

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

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

        public string SubjectState { get; set; }
    }

3.ObServer:

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

4.ConcreteObserver:

    public class StockObserver:Observer
    {
        public StockObserver(string name,Subject subject):base(name,subject)
        {

        }
        public override void Update()
        {
            Console.WriteLine("{0},{1},关闭股票行情,继续工作!", subject.SubjectState, name);
        }
    }
    public class NBAObserver:Observer
    {
        public NBAObserver(string name,Subject subject):base(name,subject)
        {

        }
        public override void Update()
        {
            Console.WriteLine("{0},{1},关闭NBA直播,继续工作!", subject.SubjectState, name);
        }
    }

5.客户端代码:

    class Program
    {
        static void Main(string[] args)
        {
            Boss boss = new Boss();
            StockObserver stockObserver = new StockObserver("张三",boss);
            NBAObserver nbaObserver = new NBAObserver("李四",boss);
            boss.Attach(stockObserver);
            boss.Attach(nbaObserver);
            //boss.Detach(nbaObserver);

            boss.SubjectState = "老板回来啦";
            boss.Notify();
        }
    }

 

使用委托实现观察者模式:

1.Subject:

    public interface Subject
    {
        void Notify();
        string SubjectState{get;set;}
    }

2.声明委托:

public delegate void UpdateEventHandler();

3.ConcreteSubject(以Boss为例):

    public class Boss:Subject
    {
        public UpdateEventHandler Update;
        public void Notify()
        {
            Update();
        }
        public string SubjectState { get; set; }
    }

4.ConcreteObserver:

    public class StockObserver
    {
        private string name;
        private Subject subject;
        public StockObserver(string name,Subject subject)
        {
            this.name = name;
            this.subject = subject;
        }
        public void CloseStockMarket()
        {
            Console.WriteLine("{0},{1},关闭股票行情,继续工作!", subject.SubjectState, name);
        }
    }
    public class NBAObserver
    {
        private string name;
        private Subject subject;
        public NBAObserver(string name, Subject subject)
        {
            this.name = name;
            this.subject = subject;
        }
        public void CloseNBADirectSeeding()
        {
            Console.WriteLine("{0},{1},关闭NBA直播,继续工作!", subject.SubjectState, name);
        }
    }

5.客户端代码:

    class Program
    {
        static void Main(string[] args)
        {
            Boss boss = new Boss();
            StockObserver stockObserver = new StockObserver("张三", boss);
            NBAObserver nbaObserver = new NBAObserver("李四", boss);
            //boss.Update = new UpdateEventHandler(stockObserver.CloseStockMarket);
            //boss.Update += new UpdateEventHandler(nbaObserver.CloseNBADirectSeeding);
            boss.Update = stockObserver.CloseStockMarket;
            boss.Update += nbaObserver.CloseNBADirectSeeding;

            boss.SubjectState = "老板回来啦";
            boss.Notify();
        }
    }

你可能感兴趣的:(观察者模式)