通用接口 [英] Generic Interface

查看:121
本文介绍了通用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想定义一个代表对远程服务的调用的接口。现在,对远程服务的调用通常返回一些东西,但也可能包括输入参数。假设一个实现类通常只实现一个服务方法。鉴于上述信息,以下是一个糟糕的设计(它感觉不正确):

  public interface IExecutesService< A ,B个
{
public A executeService();
public一个executeService(B inputParameter);
}

现在,我们用一个执行一个远程服务与输入参数:

  public class ServiceA实现IExecutesService< String,String> 
{
public String executeService()
{
//这个服务调用不应该被这个类执行
throw new IllegalStateException(这个方法不应该被调用对于这个班... blabla);
}

public String executeService(String inputParameter)
{
//执行一些服务
}

我有两个以上问题:


  1. 是使用通用的interace( IExecutesService< A,B> )在你想提供需要不同的输入params的子类和接口方法的返回类型的情况下是好的? / li>
  2. 如何做好上述工作?即我想在一个通用的界面( IExecutesService )下将我的服务执行程序分组;然而,一个实现类通常只会实现一种方法,而使用IllegalStateException感觉真的很丑陋。此外, IExecutesService< A,B> 中的B类型参数对于调用没有任何输入参数的服务的实现类将是冗余的。对于两个不同的服务呼叫,这似乎也是过分的创建两个单独的接口。

我将感谢您对此的意见。

Cheers。

解决方案

这是一个建议:

  public interface Service< T,U> {
T executeService(U ... args);
}

public class MyService实现Service< String,Integer> {
@Override
public String executeService(Integer ... args){
// do stuff
return null;
}
}

由于类型擦除,任何类只能够实施其中之一。这至少可以消除冗余的方法。



这不是一个不合理的界面,你提出,但我不是100%肯定了它添加了什么价值。您可能只想使用标准的 可调用 界面。它不支持参数,但该接口的一部分具有最小值(imho)。


Let's say I wanted to define an interface which represents a call to a remote service. Now, the call to the remote service generally returns something, but might also include input parameters. Suppose that an implementing class will typically only implement one service method. Given the above information, is the following a poor design (it doesn't quite feel right):

public interface IExecutesService<A,B>
{
    public A executeService();
    public A executeService(B inputParameter);
}

Now, let's say that I implement this interface with a class that executes a remote service with an input parameter:

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
  }

I have two questions regarding the above:

  1. Is the use of a generic interace (IExecutesService<A,B>) good in the case where you want to provide subclasses which require different input parmaters and return types for the interface methods?
  2. How can I do the above better? I.e. I want to group my service executors under a common interface (IExecutesService); however, an implementing class will typically only implement one of the methods, and the use of an IllegalStateException feels really ugly. Also, the B type parameter in IExecutesService<A,B> will be redundant for an implementing class that calls a service without any input parameters. It also seems overkill creating two separate interfaces for the two different service calls.

I will appreciate your comments on this.

Cheers.

解决方案

Here's one suggestion:

public interface Service<T,U> {
    T executeService(U... args);
}

public class MyService implements Service<String, Integer> {
    @Override
    public String executeService(Integer... args) {
        // do stuff
        return null;
    }
}

Because of type erasure any class will only be able to implement one of these. This eliminates the redundant method at least.

It's not an unreasonable interface that you're proposing but I'm not 100% sure of what value it adds either. You might just want to use the standard Callable interface. It doesn't support arguments but that part of the interface has the least value (imho).

这篇关于通用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆