C#设计模式- 观察者模式(2)
来源:互联网 发布:源mac地址全是0的报文 编辑:程序博客网 时间:2024/05/22 08:13
using System;using System.Collections.Generic;using System.Text;namespace 观察者模式{ class Program { static void Main(string[] args) { //前台小姐童子喆 Secretary tongzizhe = new Secretary(); //看股票的同事 StockObserver tongshi1 = new StockObserver("魏关姹", tongzizhe); StockObserver tongshi2 = new StockObserver("易管查", tongzizhe); //前台记下了两位同事 tongzizhe.Attach(tongshi1); tongzizhe.Attach(tongshi2); //发现老板回来 tongzizhe.SecretaryAction = "老板回来了!"; //通知两个同事 tongzizhe.Notify(); Console.Read(); } } //前台秘书类 class Secretary { //同事列表 private IList<StockObserver> observers = new List<StockObserver>(); private string action; //增加 public void Attach(StockObserver observer) { observers.Add(observer); } //减少 public void Detach(StockObserver observer) { observers.Remove(observer); } //通知 public void Notify() { foreach (StockObserver o in observers) o.Update(); } //前台状态 public string SecretaryAction { get { return action; } set { action = value; } } } //看股票的同事 class StockObserver { private string name; private Secretary sub; public StockObserver(string name, Secretary sub) { this.name = name; this.sub = sub; } public void Update() { Console.WriteLine("{0} {1} 关闭股票行情,继续工作!", sub.SecretaryAction, name); } }}
1 0
- C#设计模式- 观察者模式(2)
- C#设计模式- 观察者模式(1)
- C#设计模式- 观察者模式(3)
- C#设计模式- 观察者模式(4)
- C#设计模式- 观察者模式(5)
- C#设计模式观察者
- C# 观察者设计模式
- C#观察者模式设计
- c# 设计模式:观察者模式
- c#设计模式-观察者模式
- c# 设计模式:观察者模式
- c#设计模式-观察者模式
- 【C#设计模式-观察者模式】
- c#设计模式-观察者模式
- C#设计模式之观察者
- c#中的观察者设计模式
- 设计模式-2-观察者模式(2)
- Android设计模式(2)-----观察者模式
- Android Volley完全解析(四),带你从源码的角度理解Volley
- call to undefined function openssl_get_privatekey() 解決辦法
- java并发编程---sleep和wait
- sharepoint iframe
- FastDFS的配置、部署与API使用解读(1)Get Started with FastDFS
- C#设计模式- 观察者模式(2)
- 【noip 2004】 合并果子
- (OK) 编译ANDROID - ERROR - depmod: ERROR: Found 3 modules in dependency cycles!
- 远程文件拷贝命令scp的用法
- 为什么1000 == 1000返回为False,而100 == 100会返回为True?
- Mac 下gcc环境安装
- C#设计模式- 观察者模式(3)
- C#上机练习2
- POJ2718——Smallest Difference(暴力瞎搞)