MessageMediator
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Mediator
{
/**//// <summary>
/// 中介者(ConcreteMediator)
/// </summary>
public class MessageMediator : AbstractMessageMediator
{
private Dictionary<string, AbstractMessage> _dictionary = new Dictionary<string, AbstractMessage>();
/**//// <summary>
/// 注冊一個操作Message的對象
/// </summary>
/// <param name="abstractMessage">AbstractMessage</param>
public override void Register(AbstractMessage abstractMessage)
{
if (!_dictionary.ContainsKey(abstractMessage.UserId))
{
_dictionary.Add(abstractMessage.UserId, abstractMessage);
}
abstractMessage.AbstractMessageMediator = this;
}
/**//// <summary>
/// 發送Message
/// </summary>
/// <param name="from">來自UserId</param>
/// <param name="to">發送到UserId</param>
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
public override string Send(string from, string to, MessageModel mm)
{
AbstractMessage abstractMessage = _dictionary[to];
if (abstractMessage != null)
{
return abstractMessage.Insert(from, mm);
}
else
{
return null;
}
}
}
}