Let's look at the delegation and events in C #.

Source: Internet
Author: User

In a design pattern-Observer pattern, we already know how to apply the observer pattern, but through recent deep learning, we find that the Observer pattern has the following defects:

1. the abstract notification depends on the abstract observer;

2. Each specific observer must call the same method.

For example, the application of the observer mode in the data center charging system is shown in:


This design has the following problems:

1. Abstract notification users need to add all the specific objects in the abstract observer to the collection for sending notification messages to each specific observer;

2. When sending a notification message, the methods used to be notified in the specific observer must be the same; otherwise, the notification owner cannot traverse the notification;

3. There is nothing like updating the balance in the card, updating the student's computer status, and saving the student's computer records. The abstract observer is somewhat far-fetched.

After hard thinking, I found that using the delegate and event mechanism in C # To solve the above problem, the code structure is as follows:

Abstract notifiers:

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace observer mode _ delegate {public interface informer {void y (); string action {Get; Set ;}}}
Specific notifiers-normal dismounting:

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace observer mode _ delegate {public class down: informer {// declare a delegate Public Delegate void eventhandler (); // Add event update public event eventhandler update to delegate eventhandler; public void Policy () {This. update () ;}private string action; Public String action {get {Return Action ;}set {Action = value ;}}}}
Specific notifiers -- force drop:
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace observer mode _ delegate {public class forcedown: informer {// declare a delegate Public Delegate void eventhandler (); // Add an event update public event eventhandler update to the delegate eventhandler; public void Policy () {This. update () ;}private string action; Public String action {get {Return Action ;}set {Action = value ;}}}}
We do not need to abstract the observer. Instead, we need to update the internal balance of the card as follows:

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace observer mode _ delegate {public class updatebalance {protected informer DN; Public updatebalance (informer DN) {This. DN = DN;} public void modifybalance () {console. writeline ("{0 }! The card will be down and the balance will be updated! ", DN. Action );}}}
Specific observer -- Update Students' Computer status:

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace observer mode _ delegate {public class offline {protected informer DN; Public offline (informer DN) {This. DN = DN;} public void delonlineinfo () {console. writeline ("{0 }! The card is going to go offline. Delete the card information in the machine table! ", DN. Action );}}}
Specific observer -- save the Students' Computer records:

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace observer mode _ delegate {public class saverec {protected informer DN; Public saverec (informer DN) {This. DN = DN;} public void savelinerec () {console. writeline ("{0 }! The card needs to be down and the computer record information will be saved! ", DN. Action );}}}
Main method:

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace observer mode _ delegate {class program {static void main (string [] ARGs) {down DN = new down (); offline OL = new offline (DN ); updatebalance up = new updatebalance (DN); saverec sr = new saverec (DN); DN. update + = new down. eventhandler (ol. delonlineinfo); DN. update + = new down. eventhandler (up. modifybalance); DN. update + = new down. eventhandler (Sr. savelinerec); DN. action = "swipe your card to get down"; DN. notify ();}}}

The program execution result is as follows:


Through the execution results, we can find that the normal down event DN in the main method. after an update occurs, it is delegated to go down. eventhandler is bound to DN. the three methods on the update event are ol. delonlineinfo, up. modifybalance, Sr. savelinerec is executed to achieve the effect of the observer mode.

Now we can find that abstract notifiers no longer rely on abstract observers, because abstract notifiers notify specific observers by entrusting them. The methods called in specific observers do not need to be consistent, because you only need to bind the method you want to execute to the event. The specific notification does not have to have commonalities, so you do not have to abstract these three classes. The delegated event perfectly solves the above problems.

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.