开发者

Best way to handle events with multiple classes?

开发者 https://www.devze.com 2023-04-09 18:05 出处:网络
So I have a class that is basically a manager for 20+ copies of another class. What is the best way of handling the same event fired from each one of them? And is this the best way of unregistering th

So I have a class that is basically a manager for 20+ copies of another class. What is the best way of handling the same event fired from each one of them? And is this the best way of unregistering the events? Or should I be using a single EventHandler somehow?

I put together a simple example that basically does what I'm doing in my actual project.

class Manager
{
    List<Child> children = new List<Child>();

    public Manager()
    {
        for (int i = 0; i < 10; i++)
        {
            Childchild = new Child();
            child.Done += child_Done;
            items.Add(child);
            child.DoStuff();
        }
    }

    public void RemoveAll()
    {
        foreach (Child child in items)
        {
            child.Done -= child_Done;
        }
        items.Clear();
    }

    void child_Done(string sometext)
    {
     开发者_如何学Go   Console.WriteLine("child done: " + sometext);
    }
}

class Child
{
    public delegate void _Done(string sometext);
    public event _Done Done;

    public Child()
    {

    }

    public void DoStuff()
    {
        if (Done != null) { Done("finally!"); }
    }
}


The unregistration should be fine - as long as the target instance and method match it will work. I will, however, strongly advise you use a more regular event pattern, with a sender - then you will know which child is talking. For example:

public class MessageEventArgs : EventArgs {
    public MessageEventArgs(string message) {
        this.message = message;
    }
    private readonly string message;
    public string Message { get { return message; } }
}

and an:

public event EventHandler<MessageEventArgs> Done;
protected virtual void OnDone(string message) {
    var handler = Done;
    if(handler != null) handler(this, new MessageEventArgs(message));
}


Assuming you want the It might be better to add the Manager object to respond to each child when the child object raises the event:

It might be better to register for the event when adding a child and unregister when removing a child.

A quick way to do that would be to switch from List to ObservableCollection. This collection will raise an event as soon as the collection changes.

So in the constructor of the Manager, instantiate the ObservableCollection and register for the CollectionChanged event. In the handler check the event argument to see what children have been added, and removed so the manager can register for (or unregister) their event(s).


This is more or less the way.
A few suggestions to make it work a little better:

  1. Create a function that adds a Child, and at the same way registers the event.
    The same can be done with removing child + unregistering the event.
  2. Also, you can make the event get a "sender" parameter, and make the Child pass "this" to it. This way the event handler will be able to know which child is done, and remove it from the list, and/or whatever else needs to be done.
0

精彩评论

暂无评论...
验证码 换一张
取 消