The story of a man and three women [. NET Framework Program design] reading notes

Source: Internet
Author: User
Tags define empty functions
. NET Framework | notes | procedures | design | Example 11th multi-event example [Story of a man and three women]

Summary:

Applying the System.ComponentModel.EventHandlerList example in the FCL application of a type to publish multiple events



Scene: A boy has three girlfriends, each have different hobbies, girlfriend a love music, girlfriend B Love food, girlfriend C love xxx, to meet the various girlfriends, this boy must sing, cooking food, xxx.

This production program demonstrates the application of a single type of multiple events, and assumes that the man can only do one thing at a time (that is, to rule out the possibility of cooking on the side of xxx while singing or side xxx)

The following is the source code:

Using System;

Using System.ComponentModel;



Boyfriend's source code

public class boyfriend

{

Protected EventHandlerList EventList

= new EventHandlerList ();



//

Meet girlfriend a define music preferences

Using custom music events and callback functions

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 ("Boyfriend: OK, I'll sing you a {0}!") ", StrName);

Onmusic (New Musiceventargs (StrName));

}



//

Meet girlfriend B's gourmet Desire

//

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 ("Boyfriend: please eat a little I do {0}", strcatename);

Oncate (New Cateeventargs (Strcatename));

}



//

satisfy girlfriend C's XXX Desire

Using the Eventargs.empty event and the System.EventHandler callback function

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 ("Boyfriend: you are so beautiful today!");

OnXXX ();

}



public static void Main ()

{

Boyfriend bf = new Boyfriend ();



//

Console.WriteLine ("Morning girlfriend A To play:");

Gf_a GFA = new gf_a (BF);

Bf. Simulatemusic ("Love Song");

Gfa. Unregister (BF);



//

Console.WriteLine ();

Console.WriteLine ("Afternoon girlfriend B to play");

Gf_b GFB = new Gf_b (BF);

Bf. Simulatecate ("Ancestral small dessert");

Gfb. Unregister (BF);



//

Console.WriteLine ();

Console.WriteLine ("Evening girlfriend C to play");

Gf_c GFC = new Gf_c (BF);

Bf. Simulatexxx ();

Gfc. Unregister (BF);

}

}



Girlfriend A's 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 the song");

}



private void Musicmsg (Object sender, Boyfriend.musiceventargs args)

{

Switch (args. Musicname)

{

Case "Love Song":

Case "Qing song":

Console.WriteLine ("Girlfriend A: Wow, is {0} Yes, good love!"), args. Musicname);

Break

Default

Console.WriteLine ("Girlfriend A: This song has not heard Yes, listen to the Olympics!");

Break

}

}



public void unregister (boyfriend BF)

{

Boyfriend.musiceventhandler BFE = new Boyfriend.musiceventhandler (musicmsg);

Bf. Musicmsg-= BFE;

Console.WriteLine ("Girlfriend A: rest, don't fight!") ");

}

}

Girlfriend B's Source code

public class Gf_b

{

Public gf_b (boyfriend BF)

{

Bf. Catemsg + = new Boyfriend.cateeventhandler (catemsg);

Console.WriteLine ("Girlfriend B: Husband!") I'm hungry! ");

}

private void Catemsg (Object sender, Boyfriend.cateeventargs args)

{

Switch (args. Catename)

{

Case "Ancestral cookies":

Console.WriteLine ("Girlfriend B: Wow!") Honey, you are so good, {0} so delicious! ", args. Catename);

Break

Case "Cookie":

Case "instant noodles":

Console.WriteLine ("Girlfriend B: Just know you, give people make small snacks, now let people eat convenient food, 555555");

Break

Default

Console.WriteLine ("Girlfriend B: What's this stuff, haven't eaten yes");

Break

}

}

public void unregister (boyfriend BF)

{

Boyfriend.cateeventhandler e = new Boyfriend.cateeventhandler (catemsg);

Bf. Catemsg = e;

Console.WriteLine ("Girlfriend B: full, thank you Oh!");

}

}

Girlfriend C's Source code

public class Gf_c

{

Public Gf_c (boyfriend BF)

{

Bf. Xxxmsg + = new EventHandler (xxxmsg);

Console.WriteLine ("Girlfriend C: Husband!") You look so handsome today! ");

}



private void Xxxmsg (Object sender, EventArgs args)

{

Console.WriteLine ("Girlfriend C:r ...") O... O... M.. ");

}



public void unregister (boyfriend BF)

{

EventHandler e = new EventHandler (xxxmsg);

Bf. Xxxmsg = e;

Console.WriteLine ("Girlfriend C: tired, want to rest!");

}

}

/* Run Results:

Morning girlfriend A to play:
Girlfriend A: Honey, I want to hear a song.
Boyfriend: OK, I'll Sing you a love song!
Girlfriend A: Wow, it's love song Yes, good love!
Girlfriend A: Have a rest, don't fight!

Afternoon girlfriend B to play
Girlfriend B: Hubby, I'm hungry!
Boyfriend: Please have some of my heirloom cookies.
Girlfriend B: Wow! Honey, you are so good, the ancestral dessert is delicious!
Girlfriend B: Full, thank you Oh!

Night girlfriend C to play
Girlfriend C: Husband! You look so handsome today!
Boyfriend: You are so beautiful today.
Girlfriend C:r ... O... O... M...
Girlfriend C: Tired, want to have a rest!

*/

Note: 1, because the above example uses the System.ComponentModel.EventHandlerList in FCL, therefore does not have the thread security.

2. The xxx section of the above code does not define event parameters but uses System.EventArgs.Emtpy, and does not define callback functions but uses system.eventhandler; the other two events are custom. You can modify two other events

3. For more information on publishing events, custom events, and multiple event definitions, refer to the. NET Framework Program Design Reading notes _ Chapter 11th events


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.