【发布时间】:2018-07-16 19:05:13
【问题描述】:
我正在编写一个应用程序,它获取一个猜测名称列表(我们称之为entry),然后获取一个随机名称列表(nameList)并检查nameList 有多少个名称entry对,然后根据结果返回一个分类(例如 WINNER1)。这个应用程序应该能够允许执行另一种类型的游戏。每个实体对象都控制名称的捕获和分类(我对此不太满意,但我不允许更改它)。我创建了一个服务类,它实现了一个通用服务类(它有一个 getClassification 方法),如下所示..
public class NamesService implements GenericService {
public String getClassification(String[] entry, String[] nameList) {
// detailed implementation of getClassification for Names.java
}
}
所以 Names.java 将创建一个调用 NamesService.java 的 getClassification 方法。我必须在 Names.java 类中创建 NamesService 的全局实例,以便能够从其服务类调用 getClassification。是创建全局变量还是局部变量有区别,还是实例化NamesService有问题?
public class Names {
NamesService service = new NamesService();
// define other attributes and behaviours
public String getClassification(String[] entry, String[] nameList) {
service.getClassification(entry, nameList);
}
}
我这样写是因为我希望另一个游戏,比如 Numbers.java,能够通过使用 NumbersService 来实现 GenericService 来提供它自己的 getClassification 实现。
这是正确的方法还是有更好的方法?我正在尝试遵循 DDD 模式和 SOLID 设计原则。
还有一点,有必要用springboot来实现吗?我没有创建任何rest接口,所以我不确定是否需要springboot。
【问题讨论】:
标签: java spring-boot domain-driven-design solid-principles