0

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
  • 45
  • 1
  • 3
  • 9
  • 1
    I think making the sender format a message blindly is a poor choice. What if the constraint of one of the senders is a character limit (let's say 50 characters, maybe it's a pager!) How will the sender know which characters to include in the message? The first 50? That might not be ideal for getting the message across. I feel like the responsibility for formatting the message should belong to a type that knows something about the specific message. As another example, maybe one of the senders supports HTML and you want to **bold** important words. You'll need that message knowledge to do so. – itsme86 Nov 08 '17 at 23:24
  • @itsme86 this is one of the problem that I am not able to solve. In the case of an email and SMS, they both require a different message. I am having difficulties with the most suitable pattern for this scenario. – Omar Olivo Nov 08 '17 at 23:47

2 Answers2

1

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
  • 674
  • 11
  • 25
  • 1
    I like the idea but that could become a mess with all these independent subscribers, especially when trying to figure out which messaging channel supports which message. I think there should be at least some kind centralized notification dispatcher, working off abstract notifications (e.g. perhaps just a code and correlation info) rather than leveraging the event dispatch system directly from each channel. – plalx Nov 09 '17 at 01:18
0

Here I'll use decorator pattern its adds a lot of flexibility related to user settings you may send email sms telegram and so each sender separate decorator also easy to add new senders in future just implement decorator interface with send method