(1)定义委托:(百度百科样例,只有写了才有收获)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Entrust { public delegate void GreetingDelegate(string name); //定义委托 class Class1 { public void EnglishGreeting(string name) { Console.WriteLine("Good Morning." + name); } public void ChineseGreeting(string name) { Console.WriteLine("早上好." +name); } public void GreetPeople(string name, GreetingDelegate MakeGreeting) { MakeGreeting(name); } } }
测试类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Entrust { class TestMain { static void Main(string[] args) { Class1 c = new Class1(); //c.GreetPeople("San Zhang",c.EnglishGreeting); //c.GreetPeople("张三", c.ChineseGreeting); //Console.WriteLine("委托变量!"); //GreetingDelegate delegate1 = c.EnglishGreeting; //GreetingDelegate delegate2 = c.ChineseGreeting; //c.GreetPeople("张三", delegate2); //c.GreetPeople("San Zhang", delegate1); //Console.WriteLine("多个方法绑定同一个委托!"); //GreetingDelegate delegate1; //delegate1 = c.EnglishGreeting; //delegate1 += c.ChineseGreeting; //c.GreetPeople("李四",delegate1); //Console.WriteLine("直接调用委托!"); //GreetingDelegate delegate1; //delegate1 = c.EnglishGreeting; //delegate1 += c.ChineseGreeting; //delegate1("小张"); //GreetingDelegate delegate1 = new GreetingDelegate(c.EnglishGreeting); //delegate1 += c.ChineseGreeting; //delegate1("小李"); GreetingDelegate delegate1 = c.EnglishGreeting; delegate1 += c.ChineseGreeting; //绑定委托 delegate1 -= c.EnglishGreeting; //解除委托 delegate1("小张"); } } }
(2)定义委托事件(封装属性和方法)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Entrust { public delegate void GreetingDelegate1(string name); class GreetingManage { public event GreetingDelegate1 MakeGreet;//这里声明一个事件 public GreetingDelegate1 delegate1; public void GreetPeople(string name) { //if (delegate1 != null) // delegate1(name); MakeGreet(name); } } }
执行的方法类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Entrust { class GreetingLanguage { public void EnglishGreeting(string name) { Console.WriteLine("Morning, " + name); } public void ChineseGreeting(string name) { Console.WriteLine("早上好, " + name); } } }
测试类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Entrust { class TestMain { static void Main(string[] args) { GreetingLanguage gl = new GreetingLanguage(); GreetingManage gm = new GreetingManage(); //gm.delegate1 = gl.EnglishGreeting; gm.MakeGreet += gl.ChineseGreeting; gm.GreetPeople("小张"); Console.ReadKey(); } } }
(3)委托弥补观察者模式的不足(若观察者不存在)详见(设计模式之—观察者模式)
通知者接口类(Subject)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Observer_Pattern.CLASS { interface Subject { void Notify(); //通知 string SubjectState { get; set; } } }
秘书类(secreter)实现通知者接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Observer_Pattern.CLASS { public delegate void EventHandler(); //声明一个委托 class Secretary:Subject { public event EventHandler Update; //声明一事件Update,类型为委托EventHandler private string action; /// <summary> /// 通知 /// </summary> public void Notify() { Update(); } public string SubjectState { get { return action; } set { action = value; } } } }
炒股工作者(stockWorker)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Observer_Pattern.CLASS { class StockObserver { private string name; private Subject sub; public StockObserver(string name, Subject sub) { this.name = name; this.sub = sub; } public void StockUpdate() { Console.WriteLine("{0} ,{1} 关闭股票软件,继续工作!",sub.SubjectState,name); } } }
看NBA工作者(NBAWorker)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Observer_Pattern.CLASS { class NBAObserver { private string name; private Subject sub; public NBAObserver(string name, Subject sub) { this.name = name; this.sub = sub; } public void NBAUpdate() { Console.WriteLine("{0} ,{1} 关闭NBA视频,继续工作!",sub.SubjectState,name); } } }
测试类(TestMain)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace ObserverPattern_Entrust_ { class TestMain { static void Main(string[] args) { Secretary mishu = new Secretary(); StockObserver worker1 = new StockObserver("小张", mishu); NBAObserver worker2 = new NBAObserver("小李", mishu); mishu.Update += worker1.StockUpdate; //绑定委托 mishu.Update += worker2.NBAUpdate; //绑定委托 mishu.SubjectState = "老板回来了"; mishu.Notify(); //调用委托 Console.ReadLine(); } } }
测试结果: