我正在寻找合适而优雅的方法来创建带有类型回调的Mediator / Subscriber架构。
假设我有一个带有'事件'的班级,即代表:
public class E
{
public delegate void SomethingHappened (float a, int b);
public delegate void ProgressFinished (int[] c);
}
现在我想创建一个Mediator类,它将向这些委托注册回调并使用提供的参数调度回调:
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。我该如何解决这个问题?
答案 0 :(得分:1)
您应采取不同的方法:让发件人发送消息,并让调解员根据其类型将调度发送给不同的处理程序。
使用泛型,这将被重构为:
// 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);
}
}