java - 根据字段值对对象进行不同操作,使用哪种 OOP 设计模式?
问题描述
我在数据库中有实体,例如 MonthPlan:
class MonthPlan {
private boolean approved;
// other fields
}
还有 REST 接口,它根据哪个程序更改实体实例来接受外部请求。例如,请求
class EditMonthPlanRequest {
private long amount;
// other fields
}
用于更改月计划金额。
我需要的是根据字段MonthPlan
的值对实体执行不同的操作。approved
例如,上述请求的代码可能如下
MonthPlan plan = getPlan(...);
if (plan.isApproved()) {
// actions using data from EditMonthPlanRequest
} else {
// other actions using data from EditMonthPlanRequest
}
approved
根据已编辑实体的字段值,将有 5-6 个不同的请求,每个请求都有两个动作变体。对于这样的用例,我可以使用什么 OOP 设计模式来编写更简洁的代码?
解决方案
我认为在这种简单的情况下您不需要设计模式。每个请求都会被Service层对应的方法处理。
推荐阅读
- linux - systemctl 在启动时启用和禁用自定义服务
- ios - 通过在 URL 中嵌入密码从私有 iOS 应用程序中访问受密码保护的网站有哪些安全风险?
- javascript - 如何使用 javascript 代码在网站上选择下拉元素?
- java - 内存不足问题
- c# - 如何在文本对象上修复此空引用错误?
- php - Sylius liip 想象:无法打开图像
- javascript - 悬停 jquery 无法找到我的表的行
- vba - 如果记录尚不存在,则使用 MsgBox 将表单打开到新记录或从连续表单中删除记录
- kubernetes - 掌舵有什么好处?
- arrays - 如何将数组索引值拉到powershell中的变量?