【发布时间】:2020-09-04 03:20:29
【问题描述】:
为以下用例寻找最佳设计。
用例: 在我的应用程序中,我有一个调度程序每隔一小时运行一次并公开一项服务。
调度程序每隔一小时运行一次,从特定表中检索记录,并在任何记录处理失败时发送通知。通知可以是电子邮件或短信。
此外,此服务可以被其他服务调用以通知记录处理失败(电子邮件或短信)。
失败类型为 batchFailure 或 serviceFailure。
此外,还有一些道具设置类可以启用或禁用短信和电子邮件通知。
public class Settings {
private boolean emailEnabled;
private boolean smsEnabled;
}
根据类型,电子邮件主题应包含“批量失败”或“服务失败”的主题。批处理作业和服务失败的其他内容保持不变。
我创建了以下类:-
-
主题 - 我为两者创建了一个共同的主题。
public class Notification { private int recordId; private String recordName; private String email; // Related to email private String smsNumber; // Related to sms } -
监听类
public interface RecordFailureListener { void sendNotification(Notification notification); } -
电子邮件监听器
public class EmailListener implements RecordFailureListener { void sendNotification(Notification notification) { // Code to send email here } } -
短信监听器
public class SMSListener implements RecordFailureListener { void sendNotification(Notification notification) { // Code to send SMS here } } -
服务类
public class NotificationService { List<RecordFailureListener> listeners = new ArrayList(); // This list has emailListener and smsListener void sendNotification(Notification notification) { listeners.forEach(listener -> listener.sendNotification(notification)); } }
正在从调度程序调用此通知服务以应对任何故障。此外,从暴露的失败服务。
问题:
1) 在这里,主题似乎具有电子邮件和短信所需的属性。有没有其他更好的方法让电子邮件通知具有电子邮件所需的属性,而短信通知将具有短信所需的属性?还有一些共同的属性。
2) 在调用发送电子邮件之前检查电子邮件侦听器中是否启用了电子邮件,在短信侦听器中也是如此。这是正确的地方吗?
还有其他更好的设计吗?
谢谢
【问题讨论】:
标签: java oop design-patterns