C # delegate essential exploration 6. Correct representation of entrusting subordinates to do something

Source: Internet
Author: User

// Regression example
// Leaders entrust their subordinates to do something
Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace ConsoleApplication1.SixthTest
{
Class SixthTest
{
Public void DoTest ()
{
Lead Zhu Ge = new lead ();
Console. WriteLine ("initial execution of jobs processed by Zhu Ge ");
Zhu Ge. Processing the task ();

Console. ReadLine ();
Subordinate Wei = new subordinate (Zhu Ge );
Console. WriteLine ("Zhu Ge handles task execution after Wei joins ");
Zhu Ge. Processing the task ();
}
}

Delegate string delegate task (string task content );

Class leader
{
A public delegated task is a task entrusted by the leader;

Private string leads your own task (string task content)
{
If (the task that leads the delegation! = Null)
Return the tasks entrusted by the leader (Task content );
Else
Return "No one is doing this, so I am idle! ";
}

Public void processing task ()
{
// The time is approaching
Console. WriteLine (lead your own task ("operate World of Warcraft "));
}

}

Class subordinates
{
Public subordinates (leaders of leadership subordinates)
{
// Link your work with the work entrusted by the leadership
Subordinate leaders. tasks entrusted by the leadership + = new entrusted tasks (subordinate's own work );
}

Private string subordinate work (string task content)
{
StringBuilder work content = new StringBuilder ();
If (Task content = "Operation World of Warcraft ")
{
Work content. AppendLine ("arrange device procurement ");
Work content. AppendLine ("Recruitment customer service, induction training ");
Work content. AppendLine ("advertising ");
Work content. AppendLine ("game market ");
Work content. AppendLine ("launch activity ");
Work content. AppendLine ("............ ");
}
Else if (Task content = "processing industry competition ")
{
Work content. AppendLine ("investigating competitors ");
Work content. AppendLine ("fight ");
}
Return work content. ToString ();
}

}

// The process of converting to an instance is still not smooth, but the test is finally completed. It is fun and fun to run!
// When Zhu Ge does not have Xiaowei, the result of his "operating World of Warcraft" task is "no one is doing it, and he is idle! "
// After Xiaowei became a subordinate of Zhu Ge, he accepted the task entrusted by Zhu Ge,
// Zhu Ge re-processes the "Operation World of Warcraft" task, and the result is no longer in a daze. Someone is helping with the work.

// Compare the working principle of putting a Button on the Form. The Form internally defines the Button instance button1, and the Button event is processed as private.
// The constructor of the subordinates is passed into the lead instance. This definition aims to put the lead instance in the test code to facilitate the situation before the event is not bound.
// The lead's task processing is published to test the running status.
// This example differs from Form-Button only to disclose the execution of the Button class.

// In the whole process, Zhu Ge is only busy with his own tasks. During this period, check the tasks entrusted by him.
// After Joining Wei, she took over the entrusted task and completed the tasks that the leaders had explained with her own abilities.
// From another point of view, although Zhu Ge is only busy with his own business, in fact, Xiao Wei's "work" is actually called by Zhu Ge.
// Observe this phenomenon. We can understand Zhu Ge's "lead his own task" method as a listener,
// Call the delegate, which can be understood as calling the method of an external object.
// It becomes the Observer design mode.

// The usage of the variable can also be used as the parameter transfer of the function. The next example will be used for this research.

}

Related Article

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.