观察者设计模式理解委托与事件

通过事件使用委托

事件在类中声明且生成,且通过使用同一个类或其他类中的委托与事件处理程序关联。包含事件的类用于发布事件。这被称为 发布器(publisher) 类。其他接受该事件的类被称为 订阅器(subscriber) 类。事件使用 发布-订阅(publisher-subscriber) 模型。
发布器(publisher) 是一个包含事件和委托定义的对象。事件和委托之间的联系也定义在这个对象中。发布器(publisher)类的对象调用这个事件,并通知其他的对象。
订阅器(subscriber) 是一个接受事件并提供事件处理程序的对象。在发布器(publisher)类中的委托调用订阅器(subscriber)类中的方法(事件处理程序)。

猫抓老鼠的案例

定义一个猫的类(被观察者)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text4
{
    //被观察者
    class Cat
    {
        public string name { get; set; } 
        public string color { get; set; }
        public Cat(string name,string color)
        {
            this.name = name;
            this.color = color;
        }
        public void CatComing()
        {
            Console.WriteLine(color + "的猫" + name + "过来了,喵喵喵");
            if (CatCome != null)
            {
                CatCome();
            }
        }
        public Action CatCome;
    }
}

定义一个老鼠的类(观察者类)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text4
{
    //观察者类
    class Mouse
    {
        public string name { get; set; }
        public string color { get; set; }
        public Mouse(string name, string color)
        {
            this.name = name;
            this.color = color;
        }
        public void RunAway()
        {
            Console.WriteLine(color + "的老鼠" + name + "说:老猫来了,我要逃跑了");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text4
{
    class Program
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat("加菲猫", "黄色");
            Mouse mouse1 = new Mouse("米奇", "黑色");
            cat.CatCome += mouse1.RunAway;
            Mouse mouse2 = new Mouse("米多","白色");
            cat.CatCome += mouse2.RunAway;
            cat.CatComing();
            Console.ReadLine();
        }
    }
}
image.png

还未涉及到事件,接下来进行下一步优化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text4
{
    //观察者类
    class Mouse
    {
        public string name { get; set; }
        public string color { get; set; }
        public Mouse(string name, string color,Cat cat )
        {
            this.name = name;
            this.color = color;
            cat.CatCome += this.RunAway;//将自身的逃跑方法注册到猫里面
        }
        public void RunAway()
        {
            Console.WriteLine(color + "的老鼠" + name + "说:老猫来了,我要逃跑了");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text4
{
    class Program
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat("加菲猫", "黄色");
            Mouse mouse1 = new Mouse("米奇", "黑色",cat );
            //cat.CatCome += mouse1.RunAway;
            Mouse mouse2 = new Mouse("米多","白色",cat );
            //cat.CatCome += mouse2.RunAway;
            cat.CatComing();
            Console.ReadLine();
        }
    }
}

image.png

注意

事件提供了一种发布订阅机制,事件不能在类外调用,只能在内部调用即事件不能在类外部触发

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text4
{
    //被观察者
    class Cat
    {
        public string name { get; set; } 
        public string color { get; set; }
        public Cat(string name,string color)
        {
            this.name = name;
            this.color = color;
        }
        public void CatComing()
        {
            Console.WriteLine(color + "的猫" + name + "过来了,喵喵喵");
            if (CatCome != null)
            {
                CatCome();
            }
        }
        public event Action CatCome;//声明一个事件,即发布一个事件
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text4
{
    //观察者类
    class Mouse
    {
        public string name { get; set; }
        public string color { get; set; }
        public Mouse(string name, string color,Cat cat )
        {
            this.name = name;
            this.color = color;
            cat.CatCome += this.RunAway;//将自身的逃跑方法注册到猫里面,订阅该事件
        }
        public void RunAway()
        {
            Console.WriteLine(color + "的老鼠" + name + "说:老猫来了,我要逃跑了");
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Text4
{
    class Program
    {
        static void Main(string[] args)
        {
            Cat cat = new Cat("加菲猫", "黄色");
            Mouse mouse1 = new Mouse("米奇", "黑色",cat );
            //cat.CatCome += mouse1.RunAway;
            Mouse mouse2 = new Mouse("米多","白色",cat );
            //cat.CatCome += mouse2.RunAway;
            cat.CatComing();
            Console.ReadLine();
        }
    }
}

image.png

你可能感兴趣的:(观察者设计模式理解委托与事件)