首页 > 解决方案 > 根据字段值对对象进行不同操作,使用哪种 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 设计模式来编写更简洁的代码?

标签: javaspringoopdesign-patterns

解决方案


我认为在这种简单的情况下您不需要设计模式。每个请求都会被Service层对应的方法处理。


推荐阅读