灵活的消息发送使用哪种设计模式? [英] Which design pattern to use for flexible Message Sending?

查看:587
本文介绍了灵活的消息发送使用哪种设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to implement a Message Sending functionality based on Roles.


  • 角色A:消息应通过电子邮件和短信发送

  • 角色B:邮件只能通过电子邮件发送

  • 角色C:邮件只能通过短信发送

  • 角色D:消息应由Twitter发送

  • Role A: Message should be sent by Email and sms
  • Role B: Message should be sent by only Email
  • Role C: Message should be sent by only sms
  • Role D: Message should be sent by Twitter

我必须适应有关哪些角色可以使用哪些消息发送功能的更改,例如我需要能够将角色B更改为也包含短信。任何角色都可能需要任何消息发送功能。

I have to accommodate for change regarding what Roles can use what Message Sending functionality, e.g. I need to be able to change Role B to also include sms. Any of the Roles may need any of the Message Sending functionality.

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

I have considered having one Interface IMessageChannel with a method SendMessage. Then three classes implementing that interface, e.g. Email, SMS and Twitter. I am thinking of using the Strategy pattern and Factory? Is this correct?

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

What Design Patterns should I consider to implement this?

推荐答案

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();
   }
}

这篇关于灵活的消息发送使用哪种设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆