我知道已經晚了,但請檢查下面的調解員執行...
public sealed class Mediator
{
private static Mediator instance = null;
private volatile object locker = new object();
private MultiDictionary<ViewModelMessages, Action<Object>> internalList =
new MultiDictionary<ViewModelMessages, Action<object>>();
#region Constructors.
/// <summary>
/// Internal constructor.
/// </summary>
private Mediator() { }
/// <summary>
/// Static constructor.
/// </summary>
static Mediator() { }
#endregion
#region Properties.
/// <summary>
/// Instantiate the singleton.
/// </summary>
public static Mediator Instance
{
get
{
if (instance == null)
instance = new Mediator();
return instance;
}
}
#endregion
#region Public Methods.
/// <summary>
/// Registers a Colleague to a specific message.
/// </summary>
/// <param name="callback">The callback to use
/// when the message it seen.</param>
/// <param name="message">The message to
/// register to.</param>
public void Register(Action<Object> callback, ViewModelMessages message)
{
internalList.AddValue(message, callback);
}
/// <summary>
/// Notify all colleagues that are registed to the
/// specific message.
/// </summary>
/// <param name="message">The message for the notify by.</param>
/// <param name="args">The arguments for the message.</param>
public void NotifyColleagues(ViewModelMessages message, object args)
{
if (internalList.ContainsKey(message))
{
// forward the message to all listeners.
foreach (Action<object> callback in internalList[message])
callback(args);
}
}
#endregion
}
該類使用Dictionary<[enum], Action<T>>
進行調解。這個班受到我的讚揚,但最初是從here。它說MVVM,但沒有理由不能在其他實現中工作。
這是一個單身介體,可以按照鏈接文章中所示使用。
我希望這有助於和遲到的答覆抱歉。
Singleton引入耦合,每個人都可以訪問單例。這被認爲是不好的。 – 2013-05-10 12:23:14