- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
假设我想定义一个表示对远程服务的调用的接口(interface)。
两种服务都有不同的请求和响应
public interface ExecutesService<T,S> {
public T executeFirstService(S obj);
public T executeSecondService(S obj);
public T executeThirdService(S obj);
public T executeFourthService(S obj);
}
现在,让我们看看实现
public class ServiceA implements ExecutesService<Response1,Request1>
{
public Response1 executeFirstService(Request1 obj)
{
//This service call should not be executed by this class
throw new UnsupportedOperationException("This method should not be called for this class");
}
public Response1 executeSecondService(Request1 obj)
{
//execute some service
}
public Response1 executeThirdService(Request1 obj)
{
//execute some service
}
public Response1 executeFourthService(Request1 obj)
{
//execute some service
}
}
public class ServiceB implements ExecutesService<Response2,Request2>
{
public Response1 executeFirstService(Request1 obj)
{
//execute some service
}
public Response1 executeSecondService(Request1 obj)
{
//This service call should not be executed by this class
throw new UnsupportedOperationException("This method should not be called for this class");
}
public Response1 executeThirdService(Request1 obj)
{
//This service call should not be executed by this class
throw new UnsupportedOperationException("This method should not be called for this class");
}
public Response1 executeFourthService(Request1 obj)
{
//execute some service
}
}
在另一个类中,根据请求中的某些值,我正在创建 ServiceA
的实例或 ServiceB
我对上述问题有疑问:
是使用通用接口(interface)ExecutesService<T,S>
在您想提供需要不同 Request
的子类的情况下很好和 Response
.
我怎样才能做得更好?
最佳答案
基本上,您的当前设计违反了open closed principle 即,如果您想添加 executeFifthService()
怎么办? ServiceA
的方法和 ServiceB
等.. 类。
更新所有服务 A、B 等并不是一个好主意。简而言之,类应该对扩展开放但对修改关闭。
相反,您可以引用以下方法:
ExecutesService 接口(interface):
public interface ExecutesService<T,S> {
public T executeService(S obj);
}
ServiceA 类:
public class ServiceA implements ExecutesService<Response1,Request1> {
List<Class> supportedListOfServices = new ArrayList<>();
//load list of classnames supported by ServiceA during startup from properties
public Response1 executeService(Request1 request1, Service service) {
if(!list.contains(Service.class)) {
throw new UnsupportedOperationException("This method should
not be called for this class");
} else {
return service.execute(request1);
}
}
}
同样,你可以实现ServiceB
服务接口(interface):
public interface Service<T,S> {
public T execute(S s);
}
FirstService类:
public class FirstService implements Service<Request1,Response1> {
public Response1 execute(Request1 req);
}
同样,你需要实现SecondService
, ThirdService
等等。
因此,在这种方法中,您基本上传递了 Service
(要实际调用,它可能是 FirstService
或 SecondService
等。)在运行时和 ServiceA
验证它是否在 supportedListOfServices
中, 如果没有抛出 UnsupportedOperationException
.
这里的重点是您不需要更新任何现有服务来添加新功能(不像您需要在 executeFifthService()
、 ServiceA
等中添加 B
的设计。) ,而您需要再添加一个名为 FifthService
的类并通过它。
关于java多服务开闭原则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410500/
我是一名优秀的程序员,十分优秀!