Design Pattern - Observer(C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
Definition
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Participants
The classes and/or objects participating in this pattern are:
- Subject (Stock)
- Knows its observers. Any number of Observer objects may observe a subject
- Provides an interface for attaching and detaching Observer objects.
- ConcreteSubject (IBM)
- Stores state of interest to ConcreteObserver
- Sends a notification to its observers when its state changes
- Observer (IInvestor)
- Defines an updating interface for objects that should be notified of changes in a subject.
- ConcreteObserver (Investor)
- Maintains a reference to a ConcreteSubject object
- Stores state that should stay consistent with the subject's
- Implements the Observer updating interface to keep its state consistent with the subject's
Sample Code in C#
This structural code demonstrates the Observer pattern in which registered objects are notified of and updated with a state change.
// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Observer Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{ using System; using System.Collections.Generic; /// <summary> /// Startup class for Structural Observer Design Pattern. /// </summary> internal static class Program { #region Methods /// <summary> /// Entry point into console application. /// </summary> private static void Main() { // Configure Observer pattern var s = new ConcreteSubject(); s.Attach(new ConcreteObserver(s, "X")); s.Attach(new ConcreteObserver(s, "Y")); s.Attach(new ConcreteObserver(s, "Z")); // Change subject and notify observers s.SubjectState = "ABC"; s.Notify(); } #endregion } /// <summary> /// The 'Subject' abstract class /// </summary> internal abstract class Subject { #region Fields /// <summary> /// The observers. /// </summary> private List<Observer> observers = new List<Observer>(); #endregion #region Public Methods and Operators /// <summary> /// The attach. /// </summary> /// <param name="observer"> /// The observer. /// </param> public void Attach(Observer observer) { this.observers.Add(observer); } /// <summary> /// The detach. /// </summary> /// <param name="observer"> /// The observer. /// </param> public void Detach(Observer observer) { this.observers.Remove(observer); } /// <summary> /// The notify. /// </summary> public void Notify() { foreach (Observer o in this.observers) { o.Update(); } } #endregion } /// <summary> /// The 'ConcreteSubject' class /// </summary> internal class ConcreteSubject : Subject { // Gets or sets subject state #region Public Properties /// <summary> /// Gets or sets the subject state. /// </summary> public string SubjectState { get; set; } #endregion } /// <summary> /// The 'Observer' abstract class /// </summary> internal abstract class Observer { #region Public Methods and Operators /// <summary> /// The update. /// </summary> public abstract void Update(); #endregion } /// <summary> /// The 'ConcreteObserver' class /// </summary> internal class ConcreteObserver : Observer { #region Fields /// <summary> /// The name. /// </summary> private string name; /// <summary> /// The observer state. /// </summary> private string observerState; /// <summary> /// The subject. /// </summary> private ConcreteSubject subject; #endregion // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="ConcreteObserver"/> class. /// </summary> /// <param name="subject"> /// The subject. /// </param> /// <param name="name"> /// The name. /// </param> public ConcreteObserver(ConcreteSubject subject, string name) { this.subject = subject; this.name = name; } #endregion #region Public Properties /// <summary> /// Gets or sets the subject. /// </summary> public ConcreteSubject Subject { get { return this.subject; } set { this.subject = value; } } #endregion #region Public Methods and Operators /// <summary> /// The update. /// </summary> public override void Update() { this.observerState = this.subject.SubjectState; Console.WriteLine("Observer {0}'s new state is {1}", this.name, this.observerState); } #endregion }}// Output:/*Observer X's new state is ABCObserver Y's new state is ABCObserver Z's new state is ABC*/
This real-world code demonstrates the Observer pattern in which registered investors are notified every time a stock changes value.
// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Observer Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{ using System; using System.Collections.Generic; /// <summary> /// The 'Observer' interface /// </summary> internal interface IInvestor { #region Public Methods and Operators /// <summary> /// The update. /// </summary> /// <param name="stock"> /// The stock. /// </param> void Update(Stock stock); #endregion } /// <summary> /// Startup class for Real-World Observer Design Pattern. /// </summary> internal static class Program { #region Methods /// <summary> /// Entry point into console application. /// </summary> private static void Main() { // Create IBM stock and attach investors var ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); // Fluctuating prices will notify investors ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; } #endregion } /// <summary> /// The 'Subject' abstract class /// </summary> internal abstract class Stock { #region Fields /// <summary> /// The investors. /// </summary> private List<IInvestor> investors = new List<IInvestor>(); /// <summary> /// The price. /// </summary> private double price; #endregion // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="Stock"/> class. /// </summary> /// <param name="symbol"> /// The symbol. /// </param> /// <param name="price"> /// The price. /// </param> protected Stock(string symbol, double price) { this.Symbol = symbol; this.price = price; } #endregion #region Public Properties /// <summary> /// Gets or sets the price. /// </summary> public double Price { get { return this.price; } set { if (this.price != value) { this.price = value; this.Notify(); } } } /// <summary> /// Gets the symbol. /// </summary> public string Symbol { get; private set; } #endregion #region Public Methods and Operators /// <summary> /// The attach. /// </summary> /// <param name="investor"> /// The investor. /// </param> public void Attach(IInvestor investor) { this.investors.Add(investor); } /// <summary> /// The detach. /// </summary> /// <param name="investor"> /// The investor. /// </param> public void Detach(IInvestor investor) { this.investors.Remove(investor); } /// <summary> /// The notify. /// </summary> public void Notify() { foreach (IInvestor investor in this.investors) { investor.Update(this); } Console.WriteLine(string.Empty); } #endregion // Gets or sets the price } /// <summary> /// The 'ConcreteSubject' class /// </summary> internal class IBM : Stock { // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="IBM"/> class. /// </summary> /// <param name="symbol"> /// The symbol. /// </param> /// <param name="price"> /// The price. /// </param> public IBM(string symbol, double price) : base(symbol, price) { } #endregion } /// <summary> /// The 'ConcreteObserver' class /// </summary> internal class Investor : IInvestor { #region Fields /// <summary> /// The name. /// </summary> private string name; #endregion // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="Investor"/> class. /// </summary> /// <param name="name"> /// The name. /// </param> public Investor(string name) { this.name = name; } #endregion // Gets or sets the stock #region Public Properties /// <summary> /// Gets or sets the stock. /// </summary> public Stock Stock { get; set; } #endregion #region Public Methods and Operators /// <summary> /// The update. /// </summary> /// <param name="stock"> /// The stock. /// </param> public void Update(Stock stock) { Console.WriteLine("Notified {0} of {1}'s " + "change to {2:C}", this.name, stock.Symbol, stock.Price); } #endregion }}// Output:/*Notified Sorros of IBM's change to $120.10Notified Berkshire of IBM's change to $120.10Notified Sorros of IBM's change to $121.00Notified Berkshire of IBM's change to $121.00Notified Sorros of IBM's change to $120.50Notified Berkshire of IBM's change to $120.50Notified Sorros of IBM's change to $120.75Notified Berkshire of IBM's change to $120.75*/
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
Design Pattern - Observer(C#)相关推荐
- Design Pattern - Visitor(C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...
- Design Pattern - State(C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...
- Design Pattern - Mediator(C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...
- Design Pattern - Memento(C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...
- Design Pattern - Iterator(C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...
- Design Pattern - Interpreter(C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...
- Design Pattern - Command (C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...
- Design Pattern - Flyweight(C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...
- Design Pattern - Proxy(C#)
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...
最新文章
- 典型用户描述及进一步需求分析
- 太优秀了!GitHub 标星 40000+ 的大佬和他们的公众号
- 我的第一个IT产品:PublicLecture@HK【My First IT Product】
- vim 编辑器命令整理
- 【渝粤题库】陕西师范大学600001物理化学(上) 作业(专升本)
- MDK调试:设置断点处,代码运行的次数
- 华三交换机ping大包命令_华三交换机常用命令
- 转:开启nginx的gzip压缩的相关参数设置
- Ubuntu 16.04服务器 配置
- RGB 透明度 对应代码
- Visualizing HBase Flushes And Compactions
- Ubuntu 串口调试
- ios vs android设计
- 2018年世界杯冠军竟然被大数据算出来了,还要比吗?
- css button自动调整位置_CSS 小技巧
- 为什么使用计算机辅助翻译工具中文译文,TCloud计算机辅助翻译工具
- 计算机专业小论文题目,计算机专业小类论文题目 计算机专业小论文题目怎样拟...
- 计算机应用基础0039答案,计算机应用基础-0039(贵州电大-课程号:5205004)参考资料...
- SkipList跳表详解
- 程序员辞职的一万个理由
热门文章
- dedecms 会员网站UID注册名转MID
- 微软正式开源Blazor ,将.NET带回到浏览器
- WebBrowser(超文本浏览框)控件默认使用IE9,IE10的方法
- php5,Apache在windows 7环境搭建
- Linux新手要了解的十个知识点
- Postgresql使用笔记
- Android 线程管理
- eclipse自定义快捷键
- golang编译错误 copying /tmp/go-build069786374/b001/exe/a.out: No such file or directory 解决方法
- 持续集成工具 Jetbrains TeamCity 简介