java - Dao 类的单一职责原则
问题描述
这是一个没有遵循单一职责原则的示例代码,
public class EmailSender
{
public void SendEmail(string customerID,
string emailNotificationType)
{
//STEP1: load customer details
//STEP2: get email content
//STEP3: send email (using SmtpClient class)
}
public string GetEmailContent(Customer customer,
string emailNotificationType)
{
// Build the email notification content
}
}
我同意,如果我需要执行以下操作,它会产生问题,
-> Changes in the way, you are loading customer details.
-> Changes to the email content because of requirement enhancements / changes.
-> If there any change in the way you are sending the email instead of using SmtpClient class or something like that.
所以我们需要应用单一责任原则来分离类。我完全同意这个原则。说我需要创建三个类
EmailSender - 只专注于发送电子邮件 CustomerRepository - 只专注于获取客户数据 EmailContentBuilder - 解析电子邮件内容
但是说如果我有一个像 CustomerDao 这样的 Dao,到目前为止,我在同一个类中拥有与 CustomerDao 相关的所有 CRUD 操作,如下所示
CustomerDao 类
- add()
- update() - get()
- getAll()
- update()
我们需要在这里应用 SingleResponsibilityPrinciple 吗?如果是这样如何申请CustomerDao类?
谢谢,
哈利
解决方案
推荐阅读
- c# - C# String 定义了两个文件路径
- mysql - 如何 sequelize.js 这个 SQL 查询
- amazon-web-services - VPC 对等互连的替代方案
- c++ - 在 Gtkmm 中打开多个不同的窗口
- google-app-engine - AppEngine Objectify Java 单元测试对象缓存错误
- java - 使用属性更改监听器来更新摆动组件
- c# - 如何在 Xamarin.Forms 中进行通知
- javascript - 悬停时的 Wordpress CSS 导航菜单生效
- android - 活动对用户可见时立即触发事件
- vhdl - VHDL 中的奇怪行为