5
\$\begingroup\$

I have a use case where I want to use both HttpRequestInterceptor and HttpResponseInterceptor in one class.


I'm asking if the following would be a good solution and what are the potential problems?

public interface HttpRequestResponseInterceptor extends HttpRequestInterceptor, HttpResponseInterceptor {}


Then I implement this interface:

public class AuthenticatingResponseInterceptor implements HttpRequestResponseInterceptor{

  private static final String TAG = AuthenticatingResponseInterceptor.class.getSimpleName();

  @Inject
  private Authenticator authenticator;
  @Inject
  private CookieStore cookieStore;
  @Inject
  private RequestCache requestCache;

  @Override
  public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
    //Implementation
  }

  @Override
  public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
    //Implementation
  }
}


The part I am unsure about it is when I pass this into 2 separate methods:

@Inject
private HttpRequestResponseInterceptor interceptor;

httpClient.addResponseInterceptor(interceptor);
httpClient.addRequestInterceptor(interceptor);

I should probably point out that the HttpRequestResponseInterceptor is a singleton. Is this a good solution?

\$\endgroup\$
4
\$\begingroup\$

You don't necessarily need to have another interface that extends the two you want to combine, you just have to say that he class implements both of them. One java class can implement as many interfaces as you desire.

Problems may arise if you have any shared data problems, but by saying you are implementing the interface you are guaranteeing that the class has the specified functions.


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyClass{


    public interface Combined extends Runnable, Callable<Object> {}

public class Mixed implements Runnable, Callable<Object> {

    @Override
    public Mixed call() throws Exception {
        System.out.println("Called Mixed");
        return this;
    }

    @Override
    public void run() {
        System.out.println("Ran Mixed");
    }

}

public class Single implements Combined{
    @Override
    public Single call() throws Exception {
        System.out.println("Called Single");
        return this;
    }

    @Override
    public void run() {
        System.out.println("Ran Single");
    }
}

/**
 * @param args
 */
public static void main(String[] args) {
    MyClass top = new MyClass();
    Mixed mixed = top.new Mixed();
    Single single = top.new Single();
    ExecutorService es = Executors.newFixedThreadPool(1);

    es.execute(mixed);
    es.execute(single);

    es.submit((Callable<Object>) mixed);
    es.submit((Callable<Object>) single);

}

}

Output is:

Ran Mixed
Ran Single
Called Mixed
Called Single
\$\endgroup\$
9
  • \$\begingroup\$ no the problem comes when instanisating. As I want to pass the same object into 2 separate methods which require 2 separate types. As shown in the last part of the code \$\endgroup\$ – jiduvah Oct 15 '12 at 16:39
  • 1
    \$\begingroup\$ Which is exactly what interfaces are for. Because it extends an interface it can be treated as that object, so there is no problem, unless you don't actually implement said interface. \$\endgroup\$ – david.tanner Oct 15 '12 at 16:50
  • \$\begingroup\$ yes but you can only instantiate one object that is instansiate as both HttpRequestResponseInterceptor and HttpRequestRequestInterceptor, unless you use my approach. Or am I wrong in saying that? \$\endgroup\$ – jiduvah Oct 15 '12 at 17:03
  • 1
    \$\begingroup\$ Absolutely wrong. The interface only guarantees that the functions it defines are present in the class that implements them. It can make your code cleaner if you have one interface that extends many, but it doesn't change anything. The code that calls your class could care less what else it has in it, unless it uses reflection it only sees the methods guaranteed by the interface. \$\endgroup\$ – david.tanner Oct 15 '12 at 18:49
  • \$\begingroup\$ Ah ok, I missed a little details out, In my case I am using google guice to inject a specific instance so I define only the interface and not the class. In any case this has cleared things up \$\endgroup\$ – jiduvah Oct 16 '12 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.