17.观察者模式(Observer Pattern)
生活随笔
收集整理的這篇文章主要介紹了
17.观察者模式(Observer Pattern)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
using System;
using System.Collections.Generic;namespace ConsoleApplication10
{/// <summary>/// 在軟件構(gòu)建過程中,我們需要為某些對象建立一種“通知依賴關(guān)系” /// ——一個對象(目標對象)的狀態(tài)發(fā)生改變,所有的依賴對象(觀察者對象)都將得到通知。/// 如果這樣的依賴關(guān)系過于緊密,將使軟件不能很好地抵御變化。使用面向?qū)ο蠹夹g(shù),可以將這種依賴關(guān)系弱化,/// 并形成一種穩(wěn)定的依賴關(guān)系。從而實現(xiàn)軟件體系結(jié)構(gòu)的松耦合。/// </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>/// 發(fā)送信息/// </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>/// 在軟件構(gòu)建過程中,我們需要為某些對象建立一種“通知依賴關(guān)系” /// ——一個對象(目標對象)的狀態(tài)發(fā)生改變,所有的依賴對象(觀察者對象)都將得到通知。/// 如果這樣的依賴關(guān)系過于緊密,將使軟件不能很好地抵御變化。使用面向?qū)ο蠹夹g(shù),可以將這種依賴關(guān)系弱化,/// 并形成一種穩(wěn)定的依賴關(guān)系。從而實現(xiàn)軟件體系結(jié)構(gòu)的松耦合。/// </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);}}}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/lgxlsm/p/4647067.html
總結(jié)
以上是生活随笔為你收集整理的17.观察者模式(Observer Pattern)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UITableVIew的性能优化-重用原
- 下一篇: 124 Binary Tree Maxi