17.观察者模式(Observer Pattern)

using System;
using System.Collections.Generic;

namespace ConsoleApplication10
{
    /// <summary>
    /// 在软件构建过程中,我们需要为某些对象建立一种“通知依赖关系” 
    /// ——一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知。
    /// 如果这样的依赖关系过于紧密,将使软件不能很好地抵御变化。使用面向对象技术,可以将这种依赖关系弱化,
    /// 并形成一种稳定的依赖关系。从而实现软件体系结构的松耦合。
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Stock ms = new Microsoft("Microsoft", 120.00);
            ms.AddObserver(new Investor("Jom"));
            ms.AddObserver(new Investor("TerryLee"));
            ms.Update();
            Console.ReadLine();
        }
    }

    /// <summary>
    /// 股票
    /// </summary>
    public abstract class Stock
    {
        private List<IObserver> observers = new List<IObserver>();

        private String _symbol;

        private double _price;

        public Stock(String symbol, double price)
        {
            this._symbol = symbol;

            this._price = price;
        }

        /// <summary>
        /// 发送信息
        /// </summary>
        public void Update()
        {
            foreach (IObserver ob in observers)
            {
                ob.SendData(this);
            }

        }

        public void AddObserver(IObserver observer)
        {
            observers.Add(observer);
        }

        public void RemoveObserver(IObserver observer)
        {
            observers.Remove(observer);
        }

        public String Symbol
        {
            get { return _symbol; }
        }

        public double Price
        {
            get { return _price; }
        }
    }

    /// <summary>
    /// 微软的股票
    /// </summary>
    public class Microsoft : Stock
    {
        public Microsoft(String symbol, double price)
            : base(symbol, price)
        { }
    }


    public interface IObserver
    {
        void SendData(Stock stock);
    }

    public class Investor : IObserver
    {
        private string _name;

        public Investor(string name)
        {
            this._name = name;
        }

        public void SendData(Stock stock)
        {
            Console.WriteLine("Notified {0} of {1}'s " + "change to {2:C}", _name, stock.Symbol, stock.Price);

        }

    }

}
using System;
using System.Collections.Generic;

namespace ConsoleApplication10
{
    /// <summary>
    /// 在软件构建过程中,我们需要为某些对象建立一种“通知依赖关系” 
    /// ——一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知。
    /// 如果这样的依赖关系过于紧密,将使软件不能很好地抵御变化。使用面向对象技术,可以将这种依赖关系弱化,
    /// 并形成一种稳定的依赖关系。从而实现软件体系结构的松耦合。
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Stock stock = new Stock("Microsoft", 120.00);
            Investor investor = new Investor("Jom");
            Investor investor1 = new Investor("Sam");
            stock.NotifyEvent += new NotifyEventHandler(investor.SendData);
            stock.NotifyEvent += new NotifyEventHandler(investor1.SendData);
            stock.Update();
            Console.ReadLine();
        }
    }

    public delegate void NotifyEventHandler(object sender);

    public class Stock
    {
        public NotifyEventHandler NotifyEvent;

        private String _symbol;

        private double _price;

        public Stock(String symbol, double price)
        {
            this._symbol = symbol;
            this._price = price;
        }

        public void Update()
        {
            OnNotifyChange();
        }

        public void OnNotifyChange()
        {
            if (NotifyEvent != null)
            {
                NotifyEvent(this);
            }

        }

        public String Symbol
        {
            get { return _symbol; }
        }

        public double Price
        {
            get { return _price; }
        }
    }

    public class Investor
    {
        private string _name;

        public Investor(string name)
        {
            this._name = name;
        }

        public void SendData(object obj)
        {
            if (obj is Stock)
            {
                Stock stock = (Stock)obj;

                Console.WriteLine("Notified {0} of {1}'s " + "change to {2:C}", _name, stock.Symbol, stock.Price);
            }

        }

    }
}
原文地址:https://www.cnblogs.com/lgxlsm/p/4647067.html