zoukankan      html  css  js  c++  java
  • 观察者模式(利用委托)

    1.添加三个类文件,定义三个类(猫、狗、老鼠)

      猫是主要事件

    namespace MyDelegate
    {
        public class Cat
        {
            public CatMiaoHandler CatMiaoHandlerMethod;//在Cat类实例化一个委托
            public void Miao()
            {
                Console.WriteLine("猫,叫一声");
                if (CatMiaoHandlerMethod!=null)//若委托不为空,执行委托
                {
                    CatMiaoHandlerMethod();
                }
            }
        }
        public delegate void CatMiaoHandler();
    }
    namespace MyDelegate
    {
        public class Dog
        {
            public static void Wang()
            {
                Console.WriteLine("狗,叫一声");
            }
        }
    }
    namespace MyDelegate
    {
        public class Mouse
        {
            public static void Run()
            {
                Console.WriteLine("老鼠,跑");
            }
        }
    }

    2.当猫叫时,狗也叫,老鼠跑(这就是观察者模式)

      Program程序如下

    namespace MyDelegate
    {
        class Program
        {
            static void Main(string[] args)
            {
                Cat cat = new Cat();
                cat.CatMiaoHandlerMethod = new CatMiaoHandler(Dog.Wang);//委托
                cat.CatMiaoHandlerMethod += Mouse.Run;//多播委托,
                cat.Miao();
                Console.Read();
            }
        }
    }

    3.委托和事件的区别

    namespace MyDelegate
    {
        class Program
        {
            static void Main(string[] args)
            {
                Cat cat = new Cat();
                //--------------------------对比----------------------------
                //cat.CatMiaoHandlerMethodEvent = new CatMiaoHandler(Dog.Wang); //此处会报错,事件不能实例化
                //cat.CatMiaoHandlerMethodEvent = null;//此处会报错,事件不能赋null值
                //cat.CatMiaoHandlerMethodEvent();//此处会报错,事件不能被外部调用
                //cat.CatMiaoHandlerMethod = new CatMiaoHandler(Dog.Wang);//委托可以实例化
                //cat.CatMiaoHandlerMethod = null;//委托可以赋值为null
                //cat.CatMiaoHandlerMethod();//委托可以直接调用
                //--------------------------总结---------------------------
                //委托不太安全,可能会被外部清除已经赋予的委托函数
                cat.CatMiaoHandlerMethod += Dog.Wang;
                cat.CatMiaoHandlerMethod += Mouse.Run;
                cat.Miao();
                //事件只能进行-=或+=赋值,不能进行=new或=null操作
                cat.CatMiaoHandlerMethodEvent += Dog.Wang;
                cat.CatMiaoHandlerMethodEvent += Mouse.Run;
                cat.Miao();
    
                Console.Read();
            }
        }
    }

      另外,委托可以按委托调用函数的顺序查看委托函数,而事件查看不了

    foreach(CatMiaoHandler s in cat.CatMiaoHandlerMethod.GetInvocationList())
    {
      Console.WriteLine(s.Method);
    }
  • 相关阅读:
    May Lunchtime 2021 Division 1
    June Cook-Off 2021 Division 1
    Codeforces Round #733 (Div. 1 + Div. 2)
    腾讯云TDSQL MySQL版
    腾讯云TDSQL PostgreSQL版-产品优势
    腾讯云TDSQL PostgreSQL版 -应用场景
    腾讯云TDSQL PostgreSQL版 -最佳实践 |优化 SQL 语句
    腾讯云TDSQL PostgreSQL版 -最佳实践 |优化 SQL 语句
    腾讯云TDSQL监控库密码忘记问题解决实战
    腾讯云分布式数据库TDSQL在银行传统核心系统中的应用实践
  • 原文地址:https://www.cnblogs.com/wskxy/p/9195519.html
Copyright © 2011-2022 走看看