【发布时间】:2017-04-14 11:19:07
【问题描述】:
假设我想定义一个接口来表示对远程服务的调用。
两个服务有不同的请求和响应
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 的实例
我对上述内容有疑问:
在您想要提供需要不同Request 和Response 的子类的情况下,使用通用接口ExecutesService<T,S> 是否合适。
我怎样才能更好地做到以上几点?
【问题讨论】:
标签: java generics inheritance interface