一个男人和三个女人的故事[《.net框架程序设计》读书笔记

80酷酷网    80kuku.com

  .net框架|笔记|程序|设计|示例第十一章 多事件示例[一个男人和三个女人的故事]

摘要:

应用FCL中的System.ComponentModel.EventHandlerList示例一个类型中发布多事件的应用



场景:一个男生有三个女朋友,各自有不同的爱好,女朋友A爱好音乐,女朋友B爱好美食,女朋友C爱好XXX,为满足各个女朋友,此男生必须进行唱歌、烹饪食物、xxx。

以此制作程序演示单类型多事件的应用,并假设此男同时只能干一件事情(即排除一边xxx一边唱歌或一边xxx一边烹饪的可能J)

如下为源代码:

using System;

using System.ComponentModel;



//男朋友的源代码

public class BoyFriend

{

protected EventHandlerList eventList

= new EventHandlerList();



//

//满足女朋友A定义音乐喜好

//使用自定义的音乐事件及回调函数

protected static readonly object musicEventKey = new object();

public class MusicEventArgs : EventArgs

{

private string strMusicName;



public string MusicName

{

get{

return strMusicName;

}

}



public MusicEventArgs(string strMusicName)

{

this.strMusicName = strMusicName;

}

}



public delegate void MusicEventHandler(object sender, MusicEventArgs args);



public event MusicEventHandler MusicMsg

{

add

{

eventList.AddHandler(musicEventKey, value);

}

remove

{

eventList.RemoveHandler(musicEventKey, value);

}

}



protected virtual void OnMusic(MusicEventArgs e)

{

Delegate d = eventList[musicEventKey];

if(d != null)

{

d.DynamicInvoke(new Object[]{this, e});

}

}



public void SimulateMusic(string strName)

{

Console.WriteLine("男朋友:好的,我给你唱支{0}吧!", strName);

OnMusic(new MusicEventArgs(strName));

}



//

//满足女朋友B的美食欲望

//

protected static readonly object cateEventKey = new object();

public class CateEventArgs : EventArgs

{

private string strCateName;

public string CateName

{

get

{

return strCateName;

}

}



public CateEventArgs(string strCateName)

{

this.strCateName = strCateName;

}

}

public delegate void CateEventHandler(Object sender, CateEventArgs args);

public event CateEventHandler CateMsg

{

add

{

eventList.AddHandler(cateEventKey, value);

}



remove

{

eventList.RemoveHandler(cateEventKey, value);

}

}

protected void OnCate(CateEventArgs e)

{

Delegate d = eventList[cateEventKey];

if(d != null)

{

d.DynamicInvoke(new Object[]{this, e});

}

}

public void SimulateCate(string strCateName)

{

Console.WriteLine("男朋友:请吃一点我做的{0}", strCateName);

OnCate(new CateEventArgs(strCateName));

}



//

//满足女朋友C的xxx欲望

//使用EventArgs.Empty事件及System.EventHandler回调函数

protected static readonly object xxxEventKey = new object();

public event EventHandler XXXMsg

{

add

{

eventList.AddHandler(xxxEventKey, value);

}

remove

{

eventList.RemoveHandler(xxxEventKey, value);

}

}

protected virtual void OnXXX()

{

Delegate d = eventList[xxxEventKey];

if(d != null)

{

d.DynamicInvoke(new Object[]{this, EventArgs.Empty});

}

}

public void SimulateXXX()

{

Console.WriteLine("男朋友:你今天真漂亮呵!");

OnXXX();

}



public static void Main()

{

BoyFriend bf = new BoyFriend();



//

Console.WriteLine("上午 女朋友A来玩:");

GF_A gfa = new GF_A(bf);

bf.SimulateMusic("恋曲");

gfa.Unregister(bf);



//

Console.WriteLine();

Console.WriteLine("下午 女朋友B来玩");

GF_B gfb = new GF_B(bf);

bf.SimulateCate("祖传小甜点");

gfb.Unregister(bf);



//

Console.WriteLine();

Console.WriteLine("晚上 女朋友C来玩");

GF_C gfc = new GF_C(bf);

bf.SimulateXXX();

gfc.Unregister(bf);

}

}



//女朋友A的源代码

public class GF_A

{

public GF_A(BoyFriend bf)

{

bf.MusicMsg += new BoyFriend.MusicEventHandler(MusicMsg);

Console.WriteLine("女朋友A:老公!我要听歌");

}



private void MusicMsg(Object sender, BoyFriend.MusicEventArgs args)

{

switch(args.MusicName)

{

case "恋曲":

case "清歌":

Console.WriteLine("女朋友A:哇,是{0}耶,好喜欢啊!", args.MusicName);

break;

default:

Console.WriteLine("女朋友A:这首歌没听过耶,好好听奥!");

break;

}

}



public void Unregister(BoyFriend bf)

{

BoyFriend.MusicEventHandler bfe = new BoyFriend.MusicEventHandler(MusicMsg);

bf.MusicMsg -= bfe;

Console.WriteLine("女朋友A: 休息了,别吵!");

}

}

//女朋友B的源代码

public class GF_B

{

public GF_B(BoyFriend bf)

{

bf.CateMsg += new BoyFriend.CateEventHandler(CateMsg);

Console.WriteLine("女朋友B: 老公!我饿了!");

}

private void CateMsg(Object sender, BoyFriend.CateEventArgs args)

{

switch(args.CateName)

{

case "祖传小甜点":

Console.WriteLine("女朋友B: 哇!老公你真能干,{0}好好吃耶!", args.CateName);

break;

case "饼干":

case "方便面":

Console.WriteLine("女朋友B: 刚认识你时,给人家做小点心,现在让人家吃方便食品了,555555");

break;

default:

Console.WriteLine("女朋友B: 这是什么东东,没吃过耶");

break;

}

}

public void Unregister(BoyFriend bf)

{

BoyFriend.CateEventHandler e = new BoyFriend.CateEventHandler(CateMsg);

bf.CateMsg -= e;

Console.WriteLine("女朋友B: 吃饱了,谢谢你噢!");

}

}

//女朋友C的源代码

public class GF_C

{

public GF_C(BoyFriend bf)

{

bf.XXXMsg += new EventHandler(XXXMsg);

Console.WriteLine("女朋友C: 老公!你今天真帅哦!");

}



private void XXXMsg(Object sender, EventArgs args)

{

Console.WriteLine("女朋友C: R...O...O...M...");

}



public void Unregister(BoyFriend bf)

{

EventHandler e = new EventHandler(XXXMsg);

bf.XXXMsg -= e;

Console.WriteLine("女朋友C:累了,想休息了!");

}

}

/*运行结果:

上午 女朋友A来玩:
女朋友A:老公!我要听歌
男朋友:好的,我给你唱支恋曲吧!
女朋友A:哇,是恋曲耶,好喜欢啊!
女朋友A: 休息了,别吵!

下午 女朋友B来玩
女朋友B: 老公!我饿了!
男朋友:请吃一点我做的祖传小甜点
女朋友B: 哇!老公你真能干,祖传小甜点好好吃耶!
女朋友B: 吃饱了,谢谢你噢!

晚上 女朋友C来玩
女朋友C: 老公!你今天真帅哦!
男朋友:你今天真漂亮呵!
女朋友C: R...O...O...M...
女朋友C:累了,想休息了!

*/

注:1、因上例使用FCL中的System.ComponentModel.EventHandlerList,因此不具备线程安全性。

2、上述代码中xxx部分未定义事件参数而是使用System.EventArgs.Emtpy,也没有定义回调函数而是使用System.EventHandler;其他两个事件都是自定义的。可以修改其他两个事件

3、关于发布事件、自定义事件、多事件定义的详细信息,请参考《.net框架程序设计》读书笔记_第十一章 事件

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: