我遇到了一个众所周知的场景,我确实需要将一些实用程序私有函数(一开始就不应该存在)从我的服务类移动到实用程序类。我的问题是在实用程序类中使用服务方法。我尝试了以下重构:
class Utils(
val serviceIneed : ServiceIneed) {
companion object {
private val someMapper = ObjectMapperConfig.mapper
}
fun someUtility(): ResponseEntity<Any> {
// lots of stuff
return serviceIneed.someFunction()
}
}
然后这是我需要使用已移至新创建的实用程序类的方法的其他服务:
class anotherService(
private val serviceIneed: ServiceIneed
) {
fun someMethod() {
// lots of things happening
val utilityUsage = Utils(serviceIneed).someUtility()
}
}
这是解决这个问题的正确方法吗?您能否推荐任何重构服务类的方法,即只有面向服务的方法而不是辅助方法保留在我的服务类中?
谢谢