Get the App
SLTechnology News&Howtos  ›  Servers  › 

C# message event encapsulation

Shulou Source: shulou.com Published: 2022-06-02 19:02:25 10月04日 Update

I have always thought that the event handling mechanism of AS3 is very powerful, and today I tampered with it and passed the test perfectly. In AS3, use the function addEventListener to add event listening and removeEventListener to remove event listening. Using an encapsulated class library can completely end the irregular and irregular confusion in message delivery, thus achieving the clarity of code logic. It is also quite easy to change (anyone who has been a programmer knows it).

With regard to the implementation principle of this kind of library, in fact, a delegate is used to load the listening function (observer) to this delegate, of course, there are different types of messages, such as click, double-click, right-click and other different event types in the windows system, which are all implemented in this class library.

First of all, it is necessary to point out:

IEventType (interface for all event types)

Using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MsgEventLib.com {type of public interface IEventType {/ Type EventMainType {get;} String GetEventTypeName (string type);}}

Idea: the event type is the Type.Name + "_" + event name of the class. It is realized by GetEventTypeName method.

Message body delivered:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MsgEventLib.com {public class BaseEvent {public BaseEvent (string eventType, object sender, object msg) {this.eventType = eventType; this.sender = sender; this.msg = msg;} private string @ eventType; public string EventType {set {this.eventType = value } get {return this.eventType;}} private object @ sender; public Object Sender {set {this.sender = value;} get {return this.sender;}} private object @ msg; public object Msg {set {this.msg = value;} get {return this.msg }

Where: eventType is the event type name GetEventTypeName, Sender is the sender, and Msg is the message body

Implementation of event listening Manager

Using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MsgEventLib.com {public sealed class EventListener: IEventListener {private static IEventListener @ instance; public static IEventListener Instance {get {if (instance = = null) instance = new EventListener (); return instance;}} private Dictionary @ listeners Private EventListener () {@ listeners = new Dictionary ();} public void AddEventListener (string type, DeListener myListener) {if (! @ listeners.ContainsKey (type)) @ listeners.Add (type, myListener); else @ listeners [type] + = myListener } public void RemoveEventListener (string type, DeListener myListener) {if (@ listeners.ContainsKey (type)) {@ listeners [type]-= myListener; if (@ listeners [type] = = null) @ listeners.Remove (type) }} public void RemoveAllEventListener () {if (@ listeners! = null & & @ listeners.Count > 0) {List keys = new List (@ listeners.Keys); / / get all the key values for (int j = 0; j)

< keys.Count; j++) { if (@listeners[keys[j]] != null) { Delegate[] des = @listeners[keys[j]].GetInvocationList(); if (des != null && des.Length >

0) {for (int I = 0; I < des.Length; iTunes +) {@ listeners [keys [j]]-= des [I] as DeListener @ listeners.Remove (Keys [j]) } public DeListener GetDeListenerByType (string type) {if (@ listeners! = null & & @ listeners.ContainsKey (type)) {return @ listeners [type];} return null;} public void Destory () {this.RemoveAllEventListener () If (@ instance! = null) @ instance = null;}

It is worth noting that the dictionary: key is the event type (GetEventTypeName), value is the delegate (there may be multiple mounts / observers)

About the event sender (topic)

Using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MsgEventLib.com {public sealed class EventDispacterManager: IEventDispacterManager {private static IEventDispacterManager @ instance; public static IEventDispacterManager Instance {get {if (instance = = null) instance = new EventDispacterManager (); return instance }} public void Dispatch (string type, BaseEvent @ myevent) {DeListener target = EventListener.Instance.GetDeListenerByType (type); if (target! = null) {target (@ myevent);}

Results of the test

1, event type

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using MsgEventLib.com;namespace TestA.com {public class MyEventType: IEventType {private static MyEventType @ instance; public static MyEventType Instance {get {if (@ instance = = null) @ instance = new MyEventType (); return @ instance }} public Type EventMainType {get {return typeof (MyEventType);}} public string GetEventTypeName (string type) {return this.EventMainType.Name + "_" + type;} public static string CLOSE_WINDOWS = "CLOSE_WINDOWS"; public static string OTHRT_TYPE = "OTHRT_TYPE";}}

2, 3 classes (listening)

①:

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using MsgEventLib.com;namespace TestA.com {public sealed class Listeners {public Listeners () {string type = MyEventType.Instance.GetEventTypeName (MyEventType.CLOSE_WINDOWS); EventListener.Instance.AddEventListener (type, this.Li) } private void Li (BaseEvent e) {Console.WriteLine ("Listeners triggered Event {0}", e.Msg);}

②:

Using MsgEventLib.com;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestA.com {public sealed class Lis2 {public Lis2 () {string type = MyEventType.Instance.GetEventTypeName (MyEventType.CLOSE_WINDOWS); EventListener.Instance.AddEventListener (type, this.Li) } private void Li (BaseEvent e) {Console.WriteLine ("Lis2 triggered Event {0}", e.Msg);} public void RemoveLis () {string type = MyEventType.Instance.GetEventTypeName (MyEventType.CLOSE_WINDOWS); EventListener.Instance.RemoveEventListener (type, this.Li); Console.WriteLine ("Lis2 removes event listening!") ;}

③:

Using MsgEventLib.com;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestA.com {public sealed class Lis3 {public Lis3 () {string type = MyEventType.Instance.GetEventTypeName (MyEventType.OTHRT_TYPE); EventListener.Instance.AddEventListener (type, this.Li) } private void Li (BaseEvent e) {Console.WriteLine ("Lis3 triggered Event {0}", e.Msg);}

Event sender

Using System;using System.Collections.Generic;using System.Linq;using System.Text;using MsgEventLib.com;namespace TestA.com {public sealed class Dispatch {public void Dis () {string type = MyEventType.Instance.GetEventTypeName (MyEventType.CLOSE_WINDOWS); EventDispacterManager.Instance.Dispatch (type, new BaseEvent (type, this, "Event_ close your window");}

Test:

Using System

Using System.Collections.Generic

Using System.Linq

Using System.Text

Using TestA.com

Namespace TestA

{

Public class Program

{

Static void Main (string [] args)

{

Dispatch di = new Dispatch ()

Listeners li = new Listeners ()

Lis2 li2 = new Lis2 ()

Lis3 li3 = new Lis3 ()

Di.Dis ()

Li2.RemoveLis ()

Di.Dis ()

Console.Read ()

}

}

}

Results:

Because Lis3 is not listening on the event CLOSE_WINDOWS, it will not be triggered. Because Lis2 removes the event, it will not be triggered the second time.

This is only part of the large size. Please see the attachment for details.

Attachment: http://down.51cto.com/data/2366478

Tags: Event type message sender delegate test different function name result observer attachment observation encapsulation confusion subject code principle just multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux OPPO Reno Xiaomi Shulou Tech Info