zoukankan      html  css  js  c++  java
  • 观察者模式

    namespace ObserverInNET
    {
        class Program
        {
            // 委托充当订阅者接口类
            public delegate void NotifyEventHandler(object sender);
    
            // 抽象订阅号类
            public class TenXun
            {
                public NotifyEventHandler NotifyEvent;
    
                public string Symbol { get; set; }
                public string Info { get; set; }
                public TenXun(string symbol, string info)
                {
                    this.Symbol = symbol;
                    this.Info = info;
                }
    
                #region 新增对订阅号列表的维护操作
                public void AddObserver(NotifyEventHandler ob)
                {
                    NotifyEvent += ob;
                }
                public void RemoveObserver(NotifyEventHandler ob)
                {
                    NotifyEvent -= ob;
                }
    
                #endregion
    
                public void Update()
                {
                    if (NotifyEvent != null)
                    {
                        NotifyEvent(this);
                    }
                }
            }
    
            // 具体订阅号类
            public class TenXunGame : TenXun
            {
                public TenXunGame(string symbol, string info)
                    : base(symbol, info)
                {
                }
            }
    
            // 具体订阅者类
            public class Subscriber
            {
                public string Name { get; set; }
                public Subscriber(string name)
                {
                    this.Name = name;
                }
    
                public void ReceiveAndPrint(Object obj)
                {
                    TenXun tenxun = obj as TenXun;
    
                    if (tenxun != null)
                    {
                        Console.WriteLine("Notified {0} of {1}'s" + " Info is: {2}", Name, tenxun.Symbol, tenxun.Info);
                    }            
                }
            }
    
            static void Main(string[] args)
            {
                TenXun tenXun = new TenXunGame("TenXun Game", "Have a new game published ....");
                Subscriber lh =  new Subscriber("Learning Hard");
                Subscriber tom =  new Subscriber("Tom");
    
                // 添加订阅者
                tenXun.AddObserver(new NotifyEventHandler(lh.ReceiveAndPrint));
                tenXun.AddObserver(new NotifyEventHandler(tom.ReceiveAndPrint));
    
                tenXun.Update();
    
                Console.WriteLine("-----------------------------------");
                Console.WriteLine("移除Tom订阅者");
                tenXun.RemoveObserver(new NotifyEventHandler(tom.ReceiveAndPrint));
                tenXun.Update();
    
                Console.ReadLine();
            }
        }
    }
  • 相关阅读:
    python学习笔记(1)
    一些有趣的使用function
    axios构建请求池处理全局loading状态&&axios避免重复请求
    axios构建缓存池存储基础数据
    文件下载方法
    关于 JS this
    前端 JS 获取 Image 图像 宽高 尺寸
    Html CSS transform matrix3d 3D转场特效
    Github 持续化集成 工作流 Npm包自动化发布
    远程 Linux(Ubuntu 18)添加字体
  • 原文地址:https://www.cnblogs.com/gaocong/p/6824627.html
Copyright © 2011-2022 走看看