假设我想定义一个接口来表示对远程服务的调用。现在,对远程服务的调用通常返回一些东西,但也可能包括输入参数。假设实现类通常只实现一个服务方法。给定上面的信息,是下面一个糟糕的设计(它不是很正确):
public interface IExecutesService
{
public A executeService();
public A executeService(B inputParameter);
}
现在,假设我使用一个类来实现这个接口,该类使用输入参数执行远程服务:
public class ServiceA implements IExecutesService
{
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
}
我有两个问题上面:
>在你想要提供需要不同的输入参数和返回类型的接口方法的子类的情况下,是否使用泛型interace(IExecutesService< A,B>)?
>我如何更好地做上述?也就是说我想把我的服务执行器在一个共同的接口(IExecutesService);但是,实现类通常只实现其中一个方法,并且使用IllegalStateException感觉真的很丑。此外,IExecutesService< A,B>中的B类型参数将对于调用不具有任何输入参数的服务的实现类是冗余的。它也似乎为两个不同的服务调用创建两个单独的接口overkill。
我将感谢您对此的意见。
干杯。