Story of a man and three women [".NET Framework Design"

zhaozj2021-02-12  139

Chapter 11 Multi-Event Examples [A Man and Three Women's Story]

Summary:

Application of System.comPonentModel.EventHandlerlistModel.EventHandlerList in the FCL Announcement Application Application

Scenario: A boy has three girlfriends, each has different hobbies, girlfriend a hobby music, girlfriend B love food, girlfriend C hobby XXX, to meet all girlfriends, this boy must sing, cook food, xxx.

By this make program demonstration order type multi-event application, and assume that this man can only do one thing (ie, the possibility of singing or singing or singing while XXX J)

Such as the source code:

Using system;

Using system.componentmodel;

// boyfriend's source code

Public Class Boyfriend

{

Protected EventHandlerList EventList

= New EventHandlerList ();

//

/ / Meet the girlfriend a definition music preferences

// Use custom music events and callback functions

Protected static readOrthly 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 ("Boyfriend: Ok, I will sing {0}!", Strname);

Onmusic (New Musiceventargs (Strname);

}

//

/ / Meet the gadget of girlfriend 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 Cateeventrandler 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 ("Boyfriend: Please eat a little {0}", strcatename);

Oncate (New CateEventArgs (STRCATENAME);

}

//

/ / Meet the XXX desire of girlfriend C

// Use Eventargs.empty events and System.EventHandler callback functions

Protected static readOrthly 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, evenetargs.empty};

}

}

Public void simulatexxx ()

{

Console.writeline ("Boyfriend: You are so beautiful today!");

ONXXX ();

}

Public static void

Main

()

{

Boyfriend bf = new boyfriend ();

//

Console.writeline ("Age girlfriend a to play:");

GF_A GFA = New GF_A (BF);

bf.simulatemusic ("Love Sum");

GFA.unregister (bf);

//

Console.writeLine ();

Console.writeline ("Afternoon Girlfriend B is playing");

GF_B GFB = New GF_B (BF);

Bf.simulatecate ("Zu Chuan Xiaowei");

Gfb.unregister (bf);

//

Console.writeLine ();

Console.writeline ("Play at night");

GF_C GFC = New GF_C (BF);

bf.simulatexxx ();

Gfc.unregister (bf);

}

}

// girlfriend a source code

Public Class GF_A

{

Public GF_A (Boyfriend BF)

{

Bf.Musicmsg = New BoyFriend.MusiceventHandler (Musicmsg);

Console.writeline ("Girlfriend A: Husband! I want to listen to songs");

Private Void Musicmsg (Object Sender, BoyFriend.Musiceventargs Args)

{

Switch (args.musicname)

{

Case "Love Song":

Case "Qingge":

Console.writeline ("Girlfriend A: Wow, is {0}, I like it!", Args.musicname;

Break;

DEFAULT:

Console.Writeline ("Girlfriend A: This song did not hear the Lord, so he listened!");

Break;

}

}

Public void unregister (Boyfriend BF)

{

Boyfriend.Musiceventhandler Bfe = New BoyFriend.MusiceventHandler (Musicmsg);

Bf.musicmsg - = bfe;

Console.writeline ("Girlfriend A: Have a break, don't be noisy!");

}

}

// girlfriend B source code

Public Class GF_B

{

Public GF_B (Boyfriend BF)

{

Bf.catemsg = New BoyFriend.cateEventHandler (CATEMSG);

Console.writeline ("Girlfriend B: Husband! I am hungry!");

}

Private Void Catemsg (Object Sender, BoyFriend.cateEventArgs Args)

{

Switch (args.catename)

{

Case "ancestral small dessert":

Console.writeline ("Girlfriend B: Wow! Husband, you can do it, {0} eat well!", Args.catename);

Break;

Case "biscuit":

Case "instant noodles":

Console.writeline ("Girlfriend B: When you just know you, give people a small snack, now let people eat food, 555555");

Break;

DEFAULT:

Console.writeline ("Girlfriend B: What is this stuff, didn't eat it,");

Break;

}

}

Public void unregister (Boyfriend BF)

{

Boyfriend.cateEventHandler E = New BoyFriend.cateEventHandler (CATEMSG);

Bf.catemsg - = e;

Console.writeline ("Girlfriend B: Eat, thank you!");

}

}

// girlfriend C source code

Public Class GF_C

{

Public GF_C (Boyfriend BF)

{

Bf.xxxmsg = New EventHandler (xxxmsg);

Console.writeline ("Girlfriend C: Husband! You are really handsome today!");

}

Private void xxxmsg (Object Sender, Eventargs Args)

{

Console.writeline ("Girlfriend C: R ... o ... m ...");

}

Public void unregister (Boyfriend BF)

{

EventHandler E = New EventHandler (xxxmsg); bf.xxxmsg - = E;

Console.writeline ("Girlfriend C: Tired, want to rest!");

}

}

/*operation result:

Age girlfriend a to play:

Girlfriend A: Husband! I want to listen to songs

Boyfriend: Ok, I will sing a love song!

Girlfriend A: Wow, is love song, so I like it!

Girlfriend A: Rest, don't be noisy!

Afternoon girlfriend b

Girlfriend B: Husband! I am hungry!

Boyfriend: Please eat a little dessert me.

Girlfriend B: Wow! Husband, you can really do it, and the ancestors is a little dessert.

Girlfriend B: Eat, thank you!

Night girlfriend C

Girlfriend C: Husband! You are really handsome today!

Boyfriend: You are so beautiful today!

Girlfriend C: r ... o ... o ... m ...

Girlfriend C: Tired, want to rest!

* /

Note: 1. Because of the case of system.componentmodel.eventHandlerlist in the FCL, thread security is not available.

2. The XXX section in the above code is not defined to use System.EventArgs.emtpy, nor does the callback function are defined but use system.eventhandler; other two events are customized. Can modify other two events

3. For more information on issuance events, custom events, multi-event definitions, please refer to ".NET Framework Design" Reading Notes _ Chapter 11 Event

转载请注明原文地址:https://www.9cbs.com/read-7347.html

New Post(0)