When should I use delegation, why should I use it, and what are the advantages of delegation?

Source: Internet
Author: User
Tags class manager

Many people have been asking: When should I use delegation, why? What are the advantages of delegation ....

After reading the following articles, you will be able to get started with TDDTDS)

Although I do not like to talk too much about concepts

Let's take a look at the YY scenario: I like playing games very much, but I am not lucky. Every time I play games, I will be seen by the supervisor. Zhu doesn't like his staff when they go to work.

In this case, Zhu told his supervisor that after an employee is playing a game, you will deduct 20 yuan for him.

This is actually a commission. Once the boss Zhu entrusts his supervisor to discover Employees playing games, he will do one thing.

How should I write the program?

There are at least two categories. The supervisor and the employee, the supervisor, have a method to deduct money, and the employee has a method to play games, so it starts to work.

Employees:

Public class Employee
{
// Employee salary
Private int money;

// Supervisor of the employee
Private Manager;

Public Employee (Manager manager)
{
// Initialize the supervisor and salary of the employee through the constructor.
Manager = manager;

Money = 8000;

}

Public int Money
{
Get {return money ;}

Set {money = value ;}
}

Public void PlayGame ()
{
System. Diagnostics. Debug. WriteLine ("Employee: game started ..");

System. Diagnostics. Debug. WriteLine ("Employee: CS is really fun, haha! I play ...");

Manager. RemoveMoney (this );
}

}

 

Supervisor:

Public class Manager
{

Public Manager ()
{

}
// Deduct the salary
Public void RemoveMoney (Employee employee)
{
System. Diagnostics. Debug. WriteLine ("supervisor: dare to play games during work hours ");

System. Diagnostics. Debug. WriteLine ("supervisor: Check your salary:" + employee. Money );

System. Diagnostics. Debug. WriteLine ("supervisor: I am starting to deduct my salary ...");

Employee. Money-= 20;

System. Diagnostics. Debug. WriteLine ("supervisor: Finished .");

System. Diagnostics. Debug. WriteLine ("supervisor: Look at your kid's remaining salary:" + employee. Money );
}

}

The MIan method can be written like this:

 

Public static void Main (string [] args)
{
// Generate the object instance king of the supervisor class
Manager king = new Manager ();
// Generate the wind instance of the employee Class Object
Employee wind = new Employee (king );
// Employees start playing games
Wind. PlayGame ();
}

Running, the result is correct, it seems that as a programmer I am still relatively low-key, write the results are normal, not complacent ..

Employee: starting to play games ..
Employee: CS is really fun, haha! I play...
Supervisor: dare to play games during work hours
Supervisor: Check your salary: 8000
Supervisor: I started to deduct my salary...
Supervisor: buckle.
Supervisor: Look at your kid's remaining salary: 7980

 

The following is a simple analysis of this program:

1: The employee class can be created only after the supervisor class is created. The coupling is too high. That is to say, to create an employee, you must first create a supervisor ..

2: If the following changes occur in the scenario, I want the manager to replace the supervisor. We have to modify the employee class and add the manager class.

In this case, the program design is not good. We may consider the event (Special Commission) to implement it.

A delegate must be defined to allow the supervisor to monitor the employee, and then there must be an event in the employee class (the employee is also the object that inspires the event), and the supervisor must execute the event

Public delegate void Play (Object Sender, System. EventArgs e );

Public class Employee
{
Public int money = 2200; // you are too lazy to write attributes ..
Public event Play play;

Public Employee ()
{

}

Public void PlayGame ()
{
System. EventArgs e = new EventArgs ();
OnPlayGame (this, e );
}

Void OnPlayGame (object Sender, EventArgs e)
{
If (play! = Null)
{
Play (Sender, e );
}
}
}

The supervisor class needs to implement a method. This method is the same as the delegate's return, and the parameter is the same. It is called by the delegate .:

Public class Manager
{
Public Manager ()
{
}

Public void RemoveMoney (Object Sender, System. EventArgs e)
{
Employee emp = Sender as Employee;
Emp. money-= 20;
System. Diagnostics. Debug. WriteLine (emp. money );
}
}

Then the call becomes simple.

 

Manager xiaotao = new Manager ();
// Generate the employee class object instance shulin
Employee shulin = new Employee ();

// Set the delegate to specify the monitor
Shulin. play + = new Play (xiaotao. RemoveMoney );

// The employee starts playing the game and deducts the game once.
E. PlayGame ();
E. PlayGame ();

Output:

2180
2160

Summary:

Of course, we can still design decoupled classes without delegation and events, but it will increase many classes, interfaces and associations, and increase the code volume and logic complexity of the program, while in.. net, we only need a small amount of code to implement delegation and events.

Many people have been asking: When should I use delegation, why? What are the advantages of delegation ....

After reading the following articles, you will be able to get started with TDDTDS)

Although I do not like to talk too much about concepts

Let's take a look at the YY scenario: I like playing games very much, but I am not lucky. Every time I play games, I will be seen by the supervisor. Zhu doesn't like his staff when they go to work.

In this case, Zhu told his supervisor that after an employee is playing a game, you will deduct 20 yuan for him.

This is actually a commission. Once the boss Zhu entrusts his supervisor to discover Employees playing games, he will do one thing.

How should I write the program?

There are at least two categories. The supervisor and the employee, the supervisor, have a method to deduct money, and the employee has a method to play games, so it starts to work.

Employees:

Public class Employee
{
// Employee salary
Private int money;

// Supervisor of the employee
Private Manager;

Public Employee (Manager manager)
{
// Initialize the supervisor and salary of the employee through the constructor.
Manager = manager;

Money = 8000;

}

Public int Money
{
Get {return money ;}

Set {money = value ;}
}

Public void PlayGame ()
{
System. Diagnostics. Debug. WriteLine ("Employee: game started ..");

System. Diagnostics. Debug. WriteLine ("Employee: CS is really fun, haha! I play ...");

Manager. RemoveMoney (this );
}

}

 

Supervisor:

Public class Manager
{

Public Manager ()
{

}
// Deduct the salary
Public void RemoveMoney (Employee employee)
{
System. Diagnostics. Debug. WriteLine ("supervisor: dare to play games during work hours ");

System. Diagnostics. Debug. WriteLine ("supervisor: Check your salary:" + employee. Money );

System. Diagnostics. Debug. WriteLine ("supervisor: I am starting to deduct my salary ...");

Employee. Money-= 20;

System. Diagnostics. Debug. WriteLine ("supervisor: Finished .");

System. Diagnostics. Debug. WriteLine ("supervisor: Look at your kid's remaining salary:" + employee. Money );
}

}

The MIan method can be written like this:

 

Public static void Main (string [] args)
{
// Generate the object instance king of the supervisor class
Manager king = new Manager ();
// Generate the wind instance of the employee Class Object
Employee wind = new Employee (king );
// Employees start playing games
Wind. PlayGame ();
}

Running, the result is correct, it seems that as a programmer I am still relatively low-key, write the results are normal, not complacent ..

Employee: starting to play games ..
Employee: CS is really fun, haha! I play...
Supervisor: dare to play games during work hours
Supervisor: Check your salary: 8000
Supervisor: I started to deduct my salary...
Supervisor: buckle.
Supervisor: Look at your kid's remaining salary: 7980

 

The following is a simple analysis of this program:

1: The employee class can be created only after the supervisor class is created. The coupling is too high. That is to say, to create an employee, you must first create a supervisor ..

2: If the following changes occur in the scenario, I want the manager to replace the supervisor. We have to modify the employee class and add the manager class.

In this case, the program design is not good. We may consider the event (Special Commission) to implement it.

A delegate must be defined to allow the supervisor to monitor the employee, and then there must be an event in the employee class (the employee is also the object that inspires the event), and the supervisor must execute the event

Public delegate void Play (Object Sender, System. EventArgs e );

Public class Employee
{
Public int money = 2200; // you are too lazy to write attributes ..
Public event Play play;

Public Employee ()
{

}

Public void PlayGame ()
{
System. EventArgs e = new EventArgs ();
OnPlayGame (this, e );
}

Void OnPlayGame (object Sender, EventArgs e)
{
If (play! = Null)
{
Play (Sender, e );
}
}
}

The supervisor class needs to implement a method. This method is the same as the delegate's return, and the parameter is the same. It is called by the delegate .:

Public class Manager
{
Public Manager ()
{
}

Public void RemoveMoney (Object Sender, System. EventArgs e)
{
Employee emp = Sender as Employee;
Emp. money-= 20;
System. Diagnostics. Debug. WriteLine (emp. money );
}
}

Then the call becomes simple.

 

Manager xiaotao = new Manager ();
// Generate the employee class object instance shulin
Employee shulin = new Employee ();

// Set the delegate to specify the monitor
Shulin. play + = new Play (xiaotao. RemoveMoney );

// The employee starts playing the game and deducts the game once.
E. PlayGame ();
E. PlayGame ();

Output:

2180
2160

Summary:

Of course, we can still design decoupled classes without delegation and events, but it will increase many classes, interfaces and associations, and increase the code volume and logic complexity of the program, while in.. net, we only need a small amount of code to implement delegation and 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.