将委托作为 mediator/subscriber 模式的参数传递

Passing delegates as parameters for mediator/subscriber pattern

我正在寻找合适且优雅的方式来创建 Mediator/Subscriber 具有类型化回调的架构。

假设我有一个 class 和 'events' 即代表:

public class E
{
    public delegate void SomethingHappened (float a, int b);
    public delegate void ProgressFinished (int[] c);
}

现在我想创建一个 Mediator class,它将向这些委托注册回调并使用提供的参数分派回调:

public class Mediator
{
    public static void Register ( ???, Action callback)
    {
        // supplied delegate += callback
    }

    public static void Dispatch ( ???, params object[] list)
    {
        // executing supplied delegate with params: delegate(list)
    }
}

所以我可以按以下方式使用它:

// Class A:
Mediator.Register (E.SomethingHappened, OnSomethingHappened);
private void OnSomethingHappened (float a, int b)
{
    //..........
}

// Class B:
Mediator.Dispatch (E.SomethingHappened, 0.1f, 'qwe');

现在的问题是我无法将委托作为参数传递给 Register 或 Dispatch。我该如何解决这个问题?

您应该采取不同的方法:让您的发件人发送 消息,并让您的中介根据消息的类型将它们发送给不同的处理程序。

使用泛型,这将被重构为:

// handlers should be differentiated by message type
public class SomethingHappenedMessage
{
    public float A { get; set; }
    public int B { get; set; }
}

public class Mediator
{
    private readonly Dictionary<Type, object> _dict = new Dictionary<Type, object>();

    public void Register<Tmessage>(Action<Tmessage> callback)
    {
        _dict[typeof(Tmessage)] = callback;
    }

    public void Dispatch<Tmessage>(Tmessage msg)
    {
        var handler = _dict[typeof(Tmessage)] as Action<Tmessage>;
        handler(msg);
    }
}

或者,您可以为每种消息类型设置多个处理程序:

public class Mediator
{
    readonly Dictionary<Type, List<object>> _handlersByType = new Dictionary<Type, List<object>>();

    public void Register<Tmessage>(Action<Tmessage> callback)
    {
        List<object> handlers;
        if (!_handlersByType.TryGetValue(typeof(Tmessage), out handlers))
            _handlersByType[typeof(Tmessage)] = handlers = new List<object>();

        handlers.Add(callback);
    }

    public void Dispatch<Tmessage>(Tmessage msg)
    {
        List<object> handlers;
        if (!_handlersByType.TryGetValue(typeof(Tmessage), out handlers))
            return;

        foreach (Action<Tmessage> handler in handlers)
            handler(msg);
    }
}