委托 delegate
为什么要使用委托?
- 我们希望实现代码的封装,即把底层代码打包成一个工具,使用时直接使用工具,而不需要考虑工具的内部构造,也不希望有些功能需要我们去自己组装这个工具才能使用。
- 我们希望发送一个“信号”,当某件事情发生时,会对另一个对象产生影响。比如:一只蝴蝶扇动翅膀,触发一个小信号,最后引发西海岸卷起飓风。
格式
public class A
{
//1. 声明委托
//委托是一种类
public delegate void xxEventHandler(可以有参数也可以没有);
//2. 定义事件
public event xxEventHandler xxEvent;//这是一个信号
//3. 调用事件
public void Function1()
{
Console.WriteLine("function1");
xxEvent();
}
}
public class B
{
public void Method1(){}
public void Method2(){}
}
class Program
{
static void Main(string[] args)
{
var aa = new A();
var bb = new B();
aa.xxEvent += bb.Method1;
aa.xxEvent += bb.Method2;//事件绑定方法
}
}
例子1:照片滤镜
代码逻辑:
处理照片,给照片上不同的滤镜 定义“名词”:照片、照片处理器、滤镜
照片:定义照片的属性,包括存在哪里,大小多少,颜色blabla
照片处理器:载入照片load–加滤镜–储存照片
滤镜:滤镜函数
如果直接将所有滤镜放在处理照片的过程中,会出现这个问题:如果有人想用A/C滤镜,就必须把B注释掉,经常要改底层代码,就不够灵活
所以,我们使用(委托)一个滤镜池,把需要的滤镜放在滤镜池里。
接着,需要哪个滤镜,就把那个滤镜放到委托里。这一步可以在主程序里处理,而不需要改动底层代码。
即在主函数过程中,不断向滤镜池中加入(+=)需要的滤镜,最后执行处理函数。
【出处:Mosh Hamedani】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _0830_delegates
{
public class Photo
{
public static Photo Load(string path)
{
return new Photo();
}
public void Save()
{
}
}
public class PhotoProcessor
{
public delegate void PhotoFilterHandler(Photo photo);
public void Process(string path, PhotoFilterHandler filterHandler)
{
var photo = Photo.Load(path);
filterHandler(photo);
//var filters = new PhotoFilters();
//filters.ApplyBrightness(photo);
//filters.ApplyContrast(photo);
//filters.Resize(photo);
photo.Save();
}
}
public class PhotoProcessor2
{
public void Process(string path, Action<Photo> filterHandler)
{
var photo = Photo.Load(path);
filterHandler(photo);
photo.Save();
}
}
public class PhotoFilters
{
public void ApplyBrightness(Photo photo)
{
Console.WriteLine("Apply Brightness");
}
public void ApplyContrast(Photo photo)
{
Console.WriteLine("Apply ApplyContrast");
}
public void Resize(Photo photo)
{
Console.WriteLine("Resize photo");
}
}
//============================
class Program
{
static void Main(string[] args)
{
var processor = new PhotoProcessor();
var processor2 = new PhotoProcessor2();
var filters = new PhotoFilters();
PhotoProcessor.PhotoFilterHandler filterHandler = filters.ApplyBrightness;
Action<Photo> filterHandler2 = filters.ApplyBrightness;
filterHandler += filters.ApplyContrast;
filterHandler += filters.Resize;
filterHandler += RemoveRedEyeFilter;
processor.Process("xxx.jpg", filterHandler);
}
static void RemoveRedEyeFilter(Photo photo)
{
Console.WriteLine("apply remove red eye");
}
}
}
例子2:水壶烧水
逻辑:
热水器烧水,温度达到95时,警报器发出警报,提示器提示水烧开了
名词:热水器、警报器、提示器
热水器:烧水,产生一个委托:发送信息
警报器:发出警报
提示器:提示烧开
主函数:委托绑定报警函数、提示函数
在这个例子中,需要有“烧开了”这个信号,引发另一个类中的响应,此时,我们需要利用到事件event。
【出处:SkySoot】
https://www.cnblogs.com/SkySoot/archive/2012/04/05/2433639.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace _0831_practice_boiler
{
public class Boiler
{
public int temperature;
public delegate void BoiledHandler(int temperature);
public event BoiledHandler boiledEvent;
public void BoilWater()
{
for (int i = 0; i < 100; i++)
{
temperature = i;
Thread.Sleep(50);
Console.WriteLine("Boiling...");
if (temperature > 95)
{
if (boiledEvent != null)
{
boiledEvent(temperature);
}
}
if (temperature == 99)
Console.WriteLine("Water Boiled.");
}
}
}
public class Alarmer
{
public void AlarmInfo(int temperature)
{
Console.WriteLine("Alarm:!!Water is about to be Boiled!! It is already {0}℃.", temperature);
}
}
public class Displayer
{
public void BoiledInfo(int temperature)
{
Console.WriteLine("Display: Ahhhhh!! It is already {0}℃.", temperature);
}
}
class Program
{
static void Main(string[] args)
{
var boiler = new Boiler();
var alarmer = new Alarmer();
var displayer = new Displayer();
boiler.boiledEvent += alarmer.AlarmInfo;
boiler.boiledEvent += displayer.BoiledInfo;
boiler.BoilWater();
}
}
}
例子3:学生迟到
逻辑:
名词:班长、学生、女朋友
学生迟到,班长惩罚
班长通知家属,女朋友打学生
女朋友要去理论,班长还钱
班长通知女朋友已经还钱了,女朋友说谢谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _0512_三次委托
{
public class Leader
{
public delegate void EventHandler();
public event EventHandler InfoFamily;
public event EventHandler InfoGF;
public void Punish()
{
Console.WriteLine("leader punish student");
InfoFamily();
}
public void GiveMoney()
{
Console.WriteLine("leader give money to GF");
InfoGF();
}
}
public class Student
{
public delegate void EventHandler();
public event EventHandler LateInfo;
public void Late()
{
Console.WriteLine("stu is late");
LateInfo();
}
}
public class Girlfriend
{
public delegate void EventHandler();
public event EventHandler ArgueWithLeader;
public event EventHandler AskForMoney;
public void Beat()
{
Console.WriteLine("GF beats stu");
ArgueWithLeader();
}
public void Argue()
{
Console.WriteLine("argue");
AskForMoney();
}
public void Thank()
{
Console.WriteLine("GF say thank you to leader");
}
}
class Program
{
static void Main(string[] args)
{
var leader = new Leader();
var stu = new Student();
var gf = new Girlfriend();
stu.LateInfo += leader.Punish;
leader.InfoFamily += gf.Beat;
gf.ArgueWithLeader += gf.Argue;
gf.AskForMoney += leader.GiveMoney;
leader.InfoGF += gf.Thank;
stu.Late();
}
}
}