假设我想定义一个接口,表示对远程服务的调用。现在,对于远程服务的调用通常会返回某些内容,但也可能包括输入参数。假设实现类通常只实现一个服务方法。鉴于上述信息,以下设计是否不佳(感觉不太对):
public interface IExecutesService<A,B>
{
public A executeService();
public A executeService(B inputParameter);
}
现在,假设我用一个类来实现这个接口,并使用一个输入参数执行一个远程服务:
public class ServiceA implements IExecutesService<String,String>
{
public String executeService()
{
//This service call should not be executed by this class
throw new IllegalStateException("This method should not be called for this class...blabla");
}
public String executeService(String inputParameter)
{
//execute some service
}
我有两个关于上述内容的问题:
- 在想要为接口方法提供不同输入参数和返回类型的子类的情况下,使用通用接口 (
IExecutesService<A,B>
) 是否是一个好选择? - 如何更好地实现上述功能?即,我想将我的服务执行器分组到一个公共接口 (
IExecutesService
) 下;但是,实现类通常只会实现其中一种方法,而使用 IllegalStateException 感觉非常丑陋。此外,在没有任何输入参数调用服务的实现类中,IExecutesService<A,B>
中的 B 类型参数将是多余的。似乎创建两个不同的接口来处理这两种不同的服务调用也过于浪费。