哪种设计模式可用于灵活的消息发送?

时间:2013-07-20 08:04:54

标签: c# design-patterns

我需要实现基于角色的消息发送功能。

  • 角色A:应通过电子邮件和短信发送消息
  • 角色B:消息应仅通过电子邮件发送
  • 角色C:消息应仅由sms发送
  • 角色D:消息应由Twitter发送

我必须适应有关角色可以使用什么消息发送功能的更改,例如:我需要能够更改角色B以包括短信。任何角色都可能需要任何消息发送功能。

我考虑过一个接口IMessageChannel和一个方法SendMessage。然后实现该接口的三个类,例如,电子邮件,短信和推特。我在考虑使用策略模式和工厂?这是对的吗?

我应该考虑采用哪些设计模式来实现这一点?

2 个答案:

答案 0 :(得分:2)

I am thinking of below solution:
Interface IMessageSystem
{
void Send();
}
Public class Email : IMessageSystem
{
   public void Send()
  {
    console.writeline("Message From Email");
  }
}
Public class SMS : IMessageSystem
{
   public void Send()
  {
    console.writeline("Message From SMS");
  }
}
Public class Twitter : IMessageSystem
{
   public void Send()
  {
    console.writeline("Message From Twitter");
  }
}
Interface ISendMessageStrategy
{
  void SendMessages();
}
Public class SendMessageStrategyForRoleA : ISendMessageStrategy
{
   Public void SendMessages()
  {
    Email objemail = new Email();
   objemail.Send();
   SMS objSMS = new SMS();
   objSMS .Send();
   Twitter objtwitter = new Twitter();
   objtwitter.Send();   
  }
}
Public class SendMessageStrategyForRoleB : ISendMessageStrategy
{
   Public void SendMessages()
  {
   SMS objSMS = new SMS();
   objSMS .Send();
  }
}
Public class SendMessageStrategyForRoleC  : ISendMessageStrategy
{
  Public void SendMessages()
  {  
    Twitter objtwitter = new Twitter();
    objtwitter.Send();
  }
}
Public class SendMessageSystem
{    
   public ISendMessageStrategy sendMessageStrategy;
   List<Keyvaluepair<string,ISendMessageStrategy>> lstkeyval = new List<Keyvaluepair<string,ISendMessageStrategy();
   public SendMessageSystem(string role)
   {
       lstkeyval.add(new keyvaluepair<string,ISendMessageStrategy>("A",new SendMessageStrategyForRoleA()));
       lstkeyval.add(new keyvaluepair<string,ISendMessageStrategy>("B",new SendMessageStrategyForRoleB()));
       lstkeyval.add(new keyvaluepair<string,ISendMessageStrategy>("C",new SendMessageStrategyForRoleC()));
       sendMessageStrategy = lstkeyval.where(x=>x.Key == role).Value;
   }
   public void SendMessage ()
   {
       sendMessageStrategy.SendMessages();
   }
}
public class programme
{
   static void main (string[] args)
   {
     SendMessageSystem objMessage = new SendMessageSystem("A");
     objMessage.SendMessage();
   }
}

答案 1 :(得分:1)

看起来你需要“Chain of Responsibility”模式。 这个例子在java中,但它的原理是相同的。

创建一个抽象处理程序并为每个角色实现一个子类。

抽象处理程序:

class abstract Handler{
 Handler succesor;
 Role ROLE;

 Handler(Handler succesor, Role role){
     this.succesor = succesor;
     this.ROLE = role
 }

 void sendMessage(Message msg, Role role){
    if(role == ROLE){
        this.handleMessage(msg);
    } else if(succesor != null){
        succesor.sendMessage(msg, role);
    }
 }

 abstract void handleMessage(msg);
}

你做的不是像这样的具体处理程序:

class RoleAHandler extends Handler{

   RoleAHandler(Handler succesor){
        super(succesor, RoleA);
   }

   @override
   void handleMessage(msg){
        // put handlecode here
   }
}

并使用它:

// initialize
Handler handler = new RoleAHandler(null);
handler = new RoleBHandler(handler);

// usage
handler.sendMessage(new Message, RoleA);

您可以根据需要制作任意数量的角色