Observer Mode (Observer) delegate (delegate) C # simple Example

Source: Internet
Author: User

Observer Mode (Observer) delegate (delegate) C # simple Example

A few key points: the pattern makes the dependency between the target and the observation both loosely coupled and the notification automatically propagated
Example: A series of changes occurred after the player hit the enemy: After the explosion, the enemy less than 1 ....


namespace adapterpattern{public partial class Observerdelegateform:form {public Observerdelegateform ()            {InitializeComponent ();        Basedata.enemynumber = 100;  private void Btndisplay_click (object sender, EventArgs e) {explosionevent explosionevent = new            Explosionevent ();//Observer 1 Enemy Enemy = new Enemy ();//Observer 2 player P1 = new Player1 (); P1. Update + = new EventHandler (Explosionevent.explosionmax);//delegate to join Observer 1 P1. Update + = new EventHandler (enemy. decrease);//delegate added Observer 2 LISTBOX1.ITEMS.ADD (P1.        Display ());        }} public static class basedata//data broker {public static string displaystring {get; set;}    public static int Enemynumber {get; set;}  public delegate void EventHandler ();//delegate public for event handlers abstract class player {public event EventHandler      update;//declaring an event update protected virtual void Notify ()//Notification of join event execution  {if (update! = NULL) {update ();    }} public abstract string Display ();            public class Player1:player {public override string Display () {Notify ();//notify the Observer;        return basedata.displaystring + BaseData.EnemyNumber.ToString (); }} public class Explosionevent//Observer 1 {public void Explosionmax () {basedata.displays        Tring = "show Big Bang";          } public void Explosionmin () {basedata.displaystring = "show small explosions";    }        //.....    }       public class Enemy//Observer 2 {public void decrease () {basedata.enemynumber-= 1;         "The number of enemies decreased by 1"; }        //.....    }}


Observer Mode (Observer) delegate (delegate) C # simple Example

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.