Вопрос:

What is the most appropriate design pattern for a notification system with multiple delivery channels?

c# design-patterns notifications factory-pattern open-closed-principle

672 просмотра

1 ответ

23 Репутация автора

I am working on a student information where I would like to send notifications based on certain system events. For example, when a student is marked late or absent, the application will notify a list of users(parents, school admins, etc). This notification will be sent using all available methods(SMS,Email) to deliver the message. Each method is responsible for formatting the notification based on its constraints.

Here is a list of some events that will generate a notification:

  • Assignment marked missing
  • Average drops below passing grade Become
  • Student gets honor roll
  • Perfect attendance for month

I would like the message/notification to be delivered using SMS and Email. In the future, I would like to be able to add new delivery methods without violating the open/close principle.

A similar question similar question, but it does not mention anything about the design pattern to implement this system. I also looked into this question, but my problem is that each sender method needs to send a custom message.

How can I implement this system such that it will allow me to add new delivery methods and have each delivery method format the notification/message based on its capabilities? What is the most applicable design pattern for this scenario? Do I need to use the Factory Pattern to create the message for all the notifications mechanism?

Here is what I have so far...

 public class NotificationEmailMessage 
{
    public string ToEmail { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
}

public class NotificationSMSMessage  
{
    public string PhoneNumber { get; set; }
    public string Message { get; set; }
}

public interface INotificationSender
{
    void Send();
}

public class NotificationEmailSender : INotificationSender
{
    public void Send()
    {
        System.Diagnostics.Debug.Write("Sending Email");
    }
}

public class NotificationSMSSender : INotificationSender
{
    public void Send()
    {
        System.Diagnostics.Debug.Write("Sending SMS");
    }
}

public class NotificationMultiSender : INotificationSender
{
    public List<INotificationSender> _senders;

    public NotificationMultiSender()
    {
        _senders = new List<INotificationSender>();
    }

    public void Send()
    {
        _senders.ForEach(sender => sender.Send());
    }

    public void AddSender(INotificationSender sender)
    {

    }
}
Автор: Omar Olivo Источник Размещён: 08.11.2017 11:17

Ответы (1)


1 плюс

544 Репутация автора

What comes immediately to mind for me is the Observer Pattern. Hope that helps.

Edit: You could separate your message sending from the logic which determines which messages should be sent. To provide a more concrete example of what I'm talking about, try firing an event whenever a message should be sent. Then you can add event subscribers for each message sending type. That code which determines when messages need to be sent never needs to change and each time you add a new type, you just add a new subscriber.

Автор: Lauraducky Размещён: 08.11.2017 11:21
Вопросы из категории :
32x32