【发布时间】:2020-04-20 23:40:04
【问题描述】:
我有以下课程:
public class DomainClass {
private Integer value;
private Integer total;
private Integer month;
public Double getPercent(){/*Business logic*/}
}
我想对 DomainClass 对象列表执行相同的 getPercent 操作,而无需重复代码。我有 2 个想法来处理这个问题,但我不知道它们是否好。
想法 1 - 创建服务并迭代列表:
public class DomainObjectService{
....
public Double getPercent(List<DomainClass> list){
double value, total;
for(DomainClass d : list){
value += d.getValue();
total += d.getTotal();
}
// do percent operation
}
思路2——在数据库中查询操作,填充对象,调用业务方法
public class DomainObjectService{
....
public Double getPercent(){
double value, total;
.... query data with sql and set the above variables
// do percent operation
return new DomainBusiness(value, total).getPercentage();
}
我在读到,在 DDD 中,实体应该处理自己的逻辑,但在这种收集操作的情况下,它应该如何处理?
好吧,如果我的 DDD 基础知识是错误的。我想知道 Java 中 DDD 的好文章/书籍/示例。
【问题讨论】:
标签: java design-patterns domain-driven-design