Delegation and event mitigation

Source: Internet
Author: User
I. Delegation
The delegate is actually the function pointer in C ++. You can point this pointer to the type of function that is known when the delegate is defined. For example:

Public delegate void to connect to the database delegate ();
Indicates a delegate type. This delegate is called a "database connection delegate". All the functions it can point to are void-type functions with no parameters. For example:
Private void connect to the Oracle database ();
Private void connect to the SqlServer database ();

In our current program, we need to automatically connect to the database based on the database type set by the user.
This can be written in the program:
Connect to the database delegate instance = null;

Switch(Target database type)
{
  CaseDatabase type. Oracle database:
Delegated instance + =NewConnect to the database delegate (connect to the Oracle database );
    Break;
  CaseDatabase type. SqlServer database:
Delegated instance + =NewConnect to the database delegate (connect to the SqlServer database );
    Break;
}

Delegate instance (); // run the function pointed to by the delegate
Here we can execute the corresponding database connection function based on the switch above.

Delegation is a type, so it can be passed as a parameter like other types.

Another important feature of delegation is multicast)

That is to say, when executing a delegated instance, You can execute more than one function at the same time.

For example, if my program needs to connect to the Oracle database and the SQL Server database, you can write it as follows:
Delegated instance + = new connected database delegate (connected to the Oracle database );
Delegated instance + = new Connection database delegate (connect to SqlServer database );

Delegate instance (); // run the function pointed to by the delegate
In this way, two functions are executed simultaneously.

Ii. complete code of the delegated example

// DelegateTest. cs
UsingSystem;
UsingSystem. Threading;

NamespaceConsoleApp_CS
{
  Public ClassAppClass
{
    Public Delegate VoidDatabase Connection delegate ();

    Private Static VoidConnect to the Oracle database ()
{
Console. WriteLine ("connected to Oracle Database \ n ");
}

    Private Static VoidConnect to SqlServer database ()
{
Console. WriteLine ("connected to SqlServer database \ n ");
}

    Public Static VoidMain ()
{
Connect to the database and delegate the database connection;

Database Connection =NewConnect to the database delegate (connect to the Oracle database );

Console. WriteLine ("<connect to Oracle database only ");
Database Connection (); // connect only to the Oracle database

Database Connection =NewConnect to the database delegate (connect to the SqlServer database );

Console. WriteLine ("<connect to SqlServer database only ");
Database Connection (); // connect only to the SqlServer Database

Database Connection = null;

Database Connection + =NewConnect to the database delegate (connect to the Oracle database );
Database Connection + =NewConnect to the database delegate (connect to the SqlServer database );

Console. WriteLine ("<connect to two databases simultaneously ");
Database Connection ();

Console. Read ();
}
}
}

Iii. Events
It can be understood that when something happens, there will be a broadcast signal to tell everyone what happened. Some people may not care about stock incidents, while others may not care about flood incidents in other countries. In this way, you need to subscribe to events that interest you only. When an event occurs, the subscriber that subscribes to the event will receive a signal telling you that the event has occurred. You can ignore this event. This is the same as not subscribing to this event. Some people will handle this event, for example:
The company water dispenser is beside me. I am concerned about whether the water dispenser is out of water, so I subscribe to the "water dispenser is out of water" incident.
Company water dispenser. water dispenser out + = new company water dispenser. water dispenser out of commission (I. Change water );

In this way, when the water dispenser is triggered when there is no water, I will be notified to change the water, and the "water change" method of the "I" instance will be executed.
The water dispenser Class looks like this:

Public ClassWater Dispenser
{
  Private IntWater Volume = 10;
  Public Delegate VoidThe water dispenser has no water ();
  Public EventThe water dispenser has no water, and the entrusted water dispenser has no water;

  Public VoidEffluent (IntWater output)
{
Console. WriteLine ("someone connected ...... ");
Water Volume-= water volume;
Console. WriteLine ("{0} litre water remaining in the bucket", volume of water );
    If(Water volume = 0)
{
When the water dispenser is out of water ();
}
}

  Public VoidWhen the water dispenser is out of water ()
{
    If(The water dispenser is out of water! = Null)
{
Console. WriteLine ("the water dispenser is out of water. Come and change the water ...... ");
The water dispenser is out of water (); // trigger this event
}
}
}

The employee Class looks like this:

Public ClassEmployees
{
  Public VoidWater Change ()
{
Console. WriteLine ("It's so tiring to change water ...... ");
}
}

This should be the case in the Main function.

Water Dispenser company water dispenser =NewWater Dispenser ();
Employee I =NewCompany employee ();

Company water dispenser. water dispenser out + =NewWater Dispenser. The water dispenser has no water commission (I. Change water );

For(IntI = 0; I <10; I ++)
{
Company water dispenser. Effluent (1 );
Thread. Sleep (1000 );
}

Console. Read ();

Note that in the function "when the water dispenser has no water", you should judge whether "The water dispenser has no water" is null. Otherwise, do not run "The water dispenser has no water ();"

Iv. Complete Event example code

// EventTest. cs
UsingSystem;
UsingSystem. Threading;

NamespaceConsoleApp_CS
{
  Public ClassWater Dispenser
{
    Private IntWater Volume = 10;
    Public Delegate VoidThe water dispenser has no water ();
    Public EventThe water dispenser has no water, and the entrusted water dispenser has no water;

    Public VoidEffluent (IntWater output)
{
Console. WriteLine ("someone connected ...... ");
Water Volume-= water volume;
Console. WriteLine ("{0} litre water remaining in the bucket", volume of water );
      If(Water volume = 0)
{
When the water dispenser is out of water ();
}
}

    Public VoidWhen the water dispenser is out of water ()
{
      If(The water dispenser is out of water! = Null)
{
Console. WriteLine ("the water dispenser is out of water. Come and change the water ...... ");
The water dispenser is out of water (); // trigger this event
}
}
}

  Public ClassEmployees
{
    Public VoidWater Change ()
{
Console. WriteLine ("It's so tiring to change water ...... ");
}
}


  Public ClassAppClass
{
    Public Static VoidMain ()
{
Water Dispenser company water dispenser =NewWater Dispenser ();
Employee I =NewCompany employee ();

Company water dispenser. water dispenser out + =NewWater Dispenser. The water dispenser has no water commission (I. Change water );

      For(IntI = 0; I <10; I ++)
{
Company water dispenser. Effluent (1 );
Thread. Sleep (1000 );
}

Console. Read ();
}
}
}

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.