$notifier = new Notifier(new Sms());
$notifier->send ....
$notifier = new Notifier(new Mail());
$notifier->send....
send(Notify $notify)
.$notify
interface INotification
{
string Title { get; set; }
string Content { get; set; }
string From { get; set; }
string To { get; set; }
}
interface ISender
{
INotification Notification { get; set; }
SenderTypes Type { get; private set; }
void Send ( );
}
public class SenderBridge
{
private ISender sender;
public SenderBridge(ISender sender)
{
this.sender = sender;
}
public void Send(INotification notification)
{
this.sender.Notification = notification;
this.sender.Send();
}
}
public static class NotificationSenders
{
private static List<ISender> list;
public static void Send(this INotification source, SenderTypes type)
{
if(!Exist(type))
throw new Exception(string.Format("Sender from {0} not found",type.ToString());
var sender = Get(type);
sender.Notification = source;
sender.Send();
}
public static ISender Get(SenderTypes type)
{
return list.Where(s => s.Type.Equals(type)).FirstOrDefault();
}
public static bool Exist(SenderTypes type)
{
return list.Any(s => s.Type.Equals(type));
}
public static void AddSender(ISender sender)
{
if ( !list.Any(s => s.Type.Equals(sender.Type)) )
list.Add(sender);
else
throw new Exception("Duplicate senders");
}
public static void Remove(SenderTypes type)
{
var sender = list.Where(s => s.Type.Equals(type)).FirstOrDefault();
if ( sender != null )
list.Remove(sender);
}
public static void Clear()
{
list.Clear();
}
}
public enum SenderTypes
{
EMail,
SMS
}