zoukankan      html  css  js  c++  java
  • 设计模式学习笔记--观察者模式

     1 namespace Observer
     2 {
     3     public interface Subject
     4     {
     5         void Notify();
     6 
     7         string SubjectState
     8         {
     9             get;
    10             set;
    11         }
    12     }
    13 }
    View Code
     1 using System;
     2 
     3 namespace Observer
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/28 8:46:39 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Boss说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Boss:Subject
    12     {
    13         public event EventHandler Update;
    14 
    15         private string action;
    16 
    17         public void Notify()
    18         {
    19             Update();
    20         }
    21 
    22         public string SubjectState
    23         {
    24             get
    25             {
    26                 return action;
    27             }
    28             set
    29             {
    30                 action = value;
    31             }
    32         }
    33     }
    34 }
    View Code
     1 using System;
     2 
     3 namespace Observer
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/28 8:48:31 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// Secretary说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class Secretary:Subject
    12     {
    13         public event EventHandler Update;
    14 
    15         private string action;
    16 
    17         public void Notify()
    18         {
    19             Update();
    20         }
    21 
    22         public string SubjectState
    23         {
    24             get
    25             {
    26                 return action;
    27             }
    28             set
    29             {
    30                 action = value;
    31             }
    32         }
    33     }
    34 }
    View Code
    1 using System;
    2 
    3 namespace Observer
    4 {
    5     public delegate void EventHandler();
    6 }
    View Code
     1 using System;
     2 
     3 namespace Observer
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/28 8:37:06 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// StockObserver说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class StockObserver
    12     {
    13         private string name;
    14         private Subject subject;
    15 
    16         public StockObserver(string name,Subject subject)
    17         {
    18             this.name = name;
    19             this.subject = subject;
    20         }
    21 
    22         public void CloseStockMarket()
    23         {
    24             Console.WriteLine("{0}{1} 关闭股票行情,继续工作!",subject.SubjectState,name);
    25         }
    26     }
    27 }
    View Code
     1 using System;
     2 
     3 namespace Observer
     4 {
     5     /// <summary> 
     6     /// 作者:bzyzhang
     7     /// 时间:2016/5/28 8:42:40 
     8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
     9     /// NBAObserver说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
    10     /// </summary> 
    11     public class NBAObserver
    12     {
    13         private string name;
    14         private Subject subject;
    15 
    16         public NBAObserver(string name,Subject subject)
    17         {
    18             this.name = name;
    19             this.subject = subject;
    20         }
    21 
    22         public void CloseNBADirectSeeding()
    23         {
    24             Console.WriteLine("{0}{1} 关闭NBA直播,继续工作!",subject.SubjectState,name);
    25         }
    26     }
    27 }
    View Code
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Observer
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Boss huhansan = new Boss();
    14 
    15             StockObserver stockObserver = new StockObserver("魏关姹",huhansan);
    16             NBAObserver nbaObserver = new NBAObserver("易管查",huhansan);
    17 
    18             huhansan.Update += new EventHandler(stockObserver.CloseStockMarket);
    19             huhansan.Update += new EventHandler(nbaObserver.CloseNBADirectSeeding);
    20 
    21             huhansan.SubjectState = "我胡汉三回来了!";
    22             huhansan.Notify();
    23         }
    24     }
    25 }
    View Code
  • 相关阅读:
    素数路径Prime Path POJ3126 素数,BFS
    Fliptile POJ3279 DFS
    Find the Multiple POJ1426
    洗牌Shuffle'm Up POJ3087 模拟
    棋盘问题 POJ1321 DFS
    抓住那只牛!Catch That Cow POJ3278 BFS
    Dungeon Master POJ2251 三维BFS
    Splitting into digits CodeForce#1104A
    Ubuntu下手动安装Nvidia显卡驱动
    最大连续子序列和
  • 原文地址:https://www.cnblogs.com/bzyzhang/p/5536767.html
Copyright © 2011-2022 走看看