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

17.观察者模式(Observer Pattern)的更多相关文章

  1. 设计模式 - 观察者模式(Observer Pattern) 详细说明

    观察者模式(Observer Pattern) 详细说明 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26583157 版权全部 ...

  2. 乐在其中设计模式(C#) - 观察者模式(Observer Pattern)

    原文:乐在其中设计模式(C#) - 观察者模式(Observer Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 观察者模式(Observer Pattern) 作者:weba ...

  3. 设计模式 - 观察者模式(Observer Pattern) 详细解释

    观察者模式(Observer Pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26583157 版权全部 ...

  4. 设计模式-观察者模式(Observer Pattern)

    观察者模式(Observer Pattern):定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使他们能够自动更新自己. 观察者 ...

  5. jQuery中的观察者模式(Observer Pattern)

    在jQuery中,on方法可以为元素绑定事件,trigger方法可以手动触发事件,围绕这2个方法,我们来体验jQuery中的观察者模式(Observer Pattern). ■ on方法绑定内置事件, ...

  6. 设计模式 - 观察者模式(Observer Pattern) Java内置 用法

    观察者模式(Observer Pattern) Java内置 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26601659 ...

  7. 二十四种设计模式:观察者模式(Observer Pattern)

    观察者模式(Observer Pattern) 介绍定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新. 示例有一个Message实体类,某些对象 ...

  8. 使用C# (.NET Core) 实现观察者模式 (Observer Pattern) 并介绍 delegate 和 event

    观察者模式 这里面综合了几本书的资料. 需求 有这么个项目: 需求是这样的: 一个气象站, 有三个传感器(温度, 湿度, 气压), 有一个WeatherData对象, 它能从气象站获得这三个数据. 还 ...

  9. php观察者模式(observer pattern)

    ... <?php /* The observer pattern implements a one-too-many dependency between objects. The objec ...

随机推荐

  1. PHP运算符:算数运算符、逻辑运算符、三目运算符、位运算符、字符串运算符。

    赋值运算符 PHP 赋值运算符用于向变量写值. PHP 中基础的赋值运算符是 "=". 这意味着右侧复制表达式会为左侧运算数设置值. _______________________ ...

  2. Android列表控件ListView详解

    ListView绝对可以称得上是Android中最常用的控件之一,几乎所有应用程序都会用到它. 由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示的时候 ...

  3. Python 类变量和成员变量

    Python 类变量和成员变量 类与对象的方法 我们已经讨论了类与对象的功能部分,现在我们来看一下它的数据部分.事实上,它们只是与类和对象的名称空间 绑定 的普通变量,即这些名称只在这些类与对象的前提 ...

  4. shell脚本实现拷贝大文件显示百分比的代码分享

    #!/bin/sh strace -q -eread cp -- "${1}" "${2}" 2>&1 \| awk '{    count += ...

  5. Course Schedule I & II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  6. Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...

  7. IPC---信号量

    一.什么是信号量 信号量的使用主要是用来保护共享资源,使得资源在一个时刻只有一个进程(线程) 所拥有. 信号量的值为正的时候,说明它空闲.所测试的线程可以锁定而使用它.若为0,说明 它被占用,测试的线 ...

  8. Debian安装python-rrdtool

    ... sudo apt-get install rrdtool sudo apt-get install librrd-dev sudo apt-get install python-dev pip ...

  9. Failed to issue method call: Unit httpd.service failed to load: No such file or directory.

    centos7修改httpd.service后运行systemctl restart httpd.service提示 Failed to issue method call: Unit httpd.s ...

  10. 修改MySQL中字段的类型和长度

    MySQL修改字段类型的命令是: mysql> alter table 表名 modify column 字段名 类型; 假设在MySQL中有一个表为:address,有一个字段为city 初始 ...