Created
October 11, 2015 09:21
-
-
Save birdinforest/ac7bf94005378bd3864e to your computer and use it in GitHub Desktop.
Event, Delegate, EventArgs in .Net Framework. A example of Observer Design Pattern in Unity3D c#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| /// <summary> | |
| /// Observer or subscriber in Observe Design Pattern | |
| /// It register(or subscribe) Boiled event in Heater at Main::Start(). When water boiled, | |
| /// it receives BoiledEventArgs and Object sender. Do logic base on those received data. | |
| /// </summary> | |
| public class Alert : MonoBehaviour { | |
| public void DoAlarm(Object sender, BoiledEventArgs e) | |
| { | |
| Heater heater = (Heater)sender; | |
| Debug.Log("Alarm >> Temprature is " + e.temperature + "(Made in " + heater.MADE_IN + " Model is " + heater.MODEL + ")"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| /// <summary> | |
| /// Subject or publisher in Observe Design Pattern | |
| /// There is a Event named Boiled. When its temperature reach to 95, it will publish that event | |
| /// to observers, which are Alert and UI_Displayer. | |
| /// </summary> | |
| public class Heater : MonoBehaviour { | |
| // Other value the event can get by Object sender. | |
| public readonly string MODEL = "RealFire_MC20"; | |
| public readonly string MADE_IN = "CHINA"; | |
| // Delegate and Event | |
| public delegate void BoiledEventHandler(Object sender, BoiledEventArgs e); | |
| public event BoiledEventHandler Boiled; | |
| // Value event can get by BoiledEventAregs e | |
| private int temperature; | |
| // Boil water | |
| public void BoilWater() | |
| { | |
| for (int i = 0; i < 100; i++) | |
| { | |
| temperature = i; | |
| if (temperature >= 97) | |
| { | |
| // Create EventArgs, contains temperature value. | |
| BoiledEventArgs e = new BoiledEventArgs(temperature); | |
| OnBoiled(e); | |
| } | |
| } | |
| } | |
| // Generate Boiled event | |
| protected virtual void OnBoiled(BoiledEventArgs e) | |
| { | |
| if(Boiled != null) | |
| { | |
| Boiled(this, e); | |
| } | |
| } | |
| } | |
| /// <summary> | |
| /// Customised EventArgs | |
| /// </summary> | |
| public class BoiledEventArgs : System.EventArgs | |
| { | |
| // Can contains other values. | |
| public readonly int temperature; | |
| public BoiledEventArgs(int temperature) | |
| { | |
| this.temperature = temperature; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| /// <summary> | |
| /// Main. Observers subscribe event here. Boil water. | |
| /// </summary> | |
| public class Main : MonoBehaviour { | |
| public Heater heater; | |
| public Alert alert; | |
| // Use this for initialization | |
| void Start () { | |
| // Assign event | |
| heater.Boiled += alert.DoAlarm; // 1st way to assign event. | |
| heater.Boiled += (new Alert()).DoAlarm; // 2nd way to assign event. | |
| heater.Boiled += new Heater.BoiledEventHandler(alert.DoAlarm); // 3rd way to assign event. | |
| heater.Boiled += UI_Displayer.ShowMsg; // By a static method. | |
| // Do boil water | |
| heater.BoilWater(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| /// <summary> | |
| /// Observer or subscriber in Observe Design Pattern | |
| /// It register(or subscribe) Boiled event in Heater at Main::Start(). When water boiled, | |
| /// it receives BoiledEventArgs and Object sender. Do logic base on those received data. | |
| /// </summary> | |
| public class UI_Displayer : MonoBehaviour { | |
| public static void ShowMsg(Object sender, BoiledEventArgs e) | |
| { | |
| Heater heater = (Heater)sender; | |
| Debug.Log("UI_Displayer >> Alarm: temprature is " + e.temperature + "(Made in " + heater.MADE_IN + " Model is " + heater.MODEL + ")"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment