Common interface

Suppose I wanted to define an interface that represents a call to a remote service. Now a call to the remote service usually returns something, but may also include input parameters. Assume that an implementing class typically implements only one service method. Given the above information, the following poor design (this is not entirely correct):

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

Now suppose I implement this interface using 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 using a generic interface ( IExecutesService<A,B> ) good when you want to provide subclasses that require different input parameters and return types for interface methods?
  2. How can I do higher is better? Those. I want to group the executors of my services under a common interface ( IExecutesService ); however, an implementing class usually implements only one of the methods, and using IllegalStateException seems really ugly. In addition, a type B parameter in IExecutesService<A,B> will be redundant for the implementing class, which invokes the service without any input parameters. It also seems unnecessary to create two separate interfaces for two different service calls.
+54
java design interface
Oct 26 '09 at 7:24
source share
5 answers

Here is 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; } } 

Due to type erasure, any class can implement only one of them. This eliminates at least the redundant method.

This is not an unreasonable interface that you offer, but I am not 100% sure what it adds. You can simply use the standard Callable interface. It does not support arguments, but this part of the interface has the least value (imho).

+84
Oct 26 '09 at 7:33
source share

Here is another suggestion:

 public interface Service<T> { T execute(); } 

using this simple interface , you can pass arguments through the constructor in specific service classes:

 public class FooService implements Service<String> { private final String input1; private final int input2; public FooService(String input1, int input2) { this.input1 = input1; this.input2 = input2; } @Override public String execute() { return String.format("'%s%d'", input1, input2); } } 
+20
Oct 26 '09 at 7:49
source share

I would stay with two different interfaces.

You said that "I want to group my service providers within a common interface ... It also seems that the excess creates two separate interfaces for two different service calls ... The class will only implement one of these interfaces"

It is not clear what is the reason that you have one interface. If you want to use it as a marker, you can simply use annotations instead.

Another point is that there is a possible case where your requirements change and methods (methods) with a different signature appear on the interface. Of course, it’s possible to use an adapter template, but it would be rather strange to see that a particular class implements the interface in, say, three ways in which two of them remove UnsupportedOperationException. Perhaps the fourth method will appear, etc.

+9
Oct 26 '09 at 8:07
source share

As an answer strictly according to your question, I support the cleitus proposal.




You can also use a token interface (without a method), say DistantCall , with several several sub-interfaces that have the exact signatures you want.

  • The common interface will mark all of them if you want to write a common code for them.
  • The number of specific interfaces can be reduced with a common cleitus signature.

Examples of reusable interfaces:

  public interface DistantCall { } public interface TUDistantCall<T,U> extends DistantCall { T execute(U... us); } public interface UDistantCall<U> extends DistantCall { void execute(U... us); } public interface TDistantCall<T> extends DistantCall { T execute(); } public interface TUVDistantCall<T, U, V> extends DistantCall { T execute(U u, V... vs); } .... 



UPDATED in response to OP comment

I did not think of any instance in the call. I thought your calling code knew that it was calling, and you just need to collect some remote calls in a common interface for some kind of common code (for example, to check all remote calls for performance reasons). In your question, I did not notice that the calling code is generic :-(

If so, I suggest you have only one interface, only one signature. A few of them will only bring more complexity, for nothing.

However, you need to ask yourself a few broader questions:
How do you ensure the correct connection between the calling and called subscribers?

This may be a consequence of this question or another question ...

+4
26 Oct '09 at 8:38
source share

If I understand correctly, do you want one class to implement several such interfaces with different input / output parameters? This will not work in Java, because generics are implemented through erasure.

The problem with Java generics is that generics are really nothing but compiler magic. At run time, classes do not contain any type information used for generic materials (class type parameters, method type parameters, interface type parameters). Therefore, even if you may have overloads of certain methods, you cannot bind them to several implementations of the interface that differ only in their parameters of a general type.

In general, I understand why you think this code has a smell. However, in order to provide you with a better solution, you will need to learn a little more about your requirements. Why are you primarily using a common interface?

+3
Oct 26 '09 at 7:41
source share



All Articles