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)
{
}
}