C # Delegate and proxy Modes

Source: Internet
Author: User

In the learning process of the design model, delegation is an incomprehensible knowledge point. Especially after learning the proxy mode, you do not know what it is. Next, let's take a look at their respective instances to see the differences between the two.


A delegate is a type of reference method. Once a method is assigned to the Delegate, the delegate has the same behavior. The use of delegate methods can be the same as any other methods, with parameters and return values.
A delegate can be an abstraction of a function, that is, a class of a function. The delegated instance object represents a specific function.


Delegated instance:

Class Program {static void Main (string [] args) {Teacher Tc = new Teacher ("instructor Zhao "); // instantiate the instructor and Student St1 = new Student ("Zhang San"); Student St2 = new Student ("Li Si"); // delegate the Student's behavior to Tc. teacherCome + = new Teacher. eventHandler (St1.StopCopyWork); Tc. teacherCome + = new Teacher. eventHandler (St2.StopWhisper); Tc. come () ;}} class Teacher {private string name; public Teacher (string name) {this. name = name;} pu Blic delegate void EventHandler (); // declare a delegate EventHandler public event EventHandler TeacherCome; // declare the event name, the event type is EventHandler public void Come () // instructor behavior {Console. writeLine ("{0} is coming", name); if (TeacherCome! = Null) // If delegate is required, execute the following program {TeacherCome () ;}} class Student {private string name; public Student (string name) {this. name = name;} public void StopWhisper () {Console. writeLine ("instructor is here, {0} Stop Talking", name);} public void StopCopyWork () {Console. writeLine ("instructor is here, {0} Stop copying homework", name );}}

Running result:



<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + tpra7csjyr2juw.vcd4kpha + authorization + 1vcS/tcShozwvcD4KPHA + authorization = "brush: java;"> class Program {static void Main (string [] args) {SchoolGirl mm = new SchoolGirl (); mm. name = "jiaojiao"; Proxy daili = new Proxy (mm); daili. giveDolls (); daili. giveFlowers (); daili. giveChocolate () ;}} class SchoolGirl // defines the girl class {private string name; public string Name {get {return name ;}set {name = value ;}}} interface IGiveGift // create an interface for sending gifts {void GiveDolls (); void GiveFlowers (); void GiveChocolate ();} class Pursuit: IGiveGift // accesser class, use the gift sending interface {SchoolGirl mm; // the girl's name public Pursuit (SchoolGirl mm) {this. mm = mm;} public void GiveDolls () {Console. writeLine (mm. name + "");} public void GiveFlowers () {Console. writeLine (mm. name + "flowers for you");} public void GiveChocolate () {Console. writeLine (mm. name + "") ;}} class Proxy: IGiveGift // defines the Proxy mode, and also uses the gift sending interface {Pursuit gg; public Proxy (SchoolGirl mm) // The difference is that {gg = new Pursuit (mm);} public void GiveDolls () {gg. giveDolls ();} public void GiveFlowers () {gg. giveFlowers ();} public void GiveChocolate () {gg. giveChocolate ();}}
Running result:



Delegate and proxy comparison

Proxy: give some help to someone.

Delegate: when something happens, do it by the way. Delegation is equivalent to a trigger.





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.