博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
观察者模式(C#实现 + eventhandler)
阅读量:6908 次
发布时间:2019-06-27

本文共 1436 字,大约阅读时间需要 4 分钟。

namespace ObserverPattern{    public interface ISubject    {        event EventHandler NotifyHandler;     }    public class CstEventArg : EventArgs    {        public string Name { get; set; }    }    public class News : ISubject    {        public event EventHandler NotifyHandler;        public void NotifyChanges(EventArgs e)        {            if (NotifyHandler != null)            {                NotifyHandler(null, e);            }        }    }    public class CommonOne    {        public CommonOne(ISubject subject)        {            subject.NotifyHandler += ShowInConsole;        }        public void ShowInConsole(object sender, EventArgs e)        {            Console.WriteLine("show in console " + (e as CstEventArg).Name);        }    }    public class CommonTwo    {        public CommonTwo(ISubject subject)        {            subject.NotifyHandler += ShowInConsole;        }        public void ShowInConsole(object sender, EventArgs e)        {            Console.WriteLine("show in WPF " + (e as CstEventArg).Name);        }    }    class Program    {        static void Main(string[] args)        {            News news = new News();            CommonOne cp1 = new CommonOne(news);            CommonTwo cp2 = new CommonTwo(news);            news.NotifyChanges(new CstEventArg() { Name = "abc" });            Console.ReadLine();        }    }}

转载于:https://www.cnblogs.com/webglcn/archive/2012/10/18/2729409.html

你可能感兴趣的文章
LNMP环境搭建-php
查看>>
Hadoop云计算的初步认识
查看>>
windows下创建控制台窗口
查看>>
JVM配置参数
查看>>
jBPM5与Activiti5比较
查看>>
iOS App 的逆向
查看>>
Spring如何扫描class和配置文件
查看>>
Java压缩技术(一) ZLib
查看>>
【VMware虚拟化解决方案】VMware Horizon View Client 各平台配置文档
查看>>
java线程池
查看>>
Linux内核线程
查看>>
Linux cp时总询问是否覆盖,怎样让它不询问直接覆盖
查看>>
笨方法学python Lesson 45
查看>>
Java HashMap的实现原理
查看>>
服务器的发送数据
查看>>
kvm install 报错could not open disk imageXXX: Permission denied
查看>>
lduan office 365 自定义域的添加和配置二
查看>>
在Wordpress侧栏中使用下拉菜单显示分类
查看>>
基础排序算法 – 选择排序Selection sort
查看>>
Appium移动自动化测试环境部署
查看>>