patternjavaMinor
Is this the best approch to combine 2 Interfaces?
Viewed 0 times
thisapprochthecombineinterfacesbest
Problem
I have a use case where I want to use both
I'm asking if the following would be a good solution and what are the potential problems?
Then I implement this interface:
The part I am unsure about it is when I pass this into 2 separate methods:
I should probably point out that the
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?Solution
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.
Output is:
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 {}
public class Mixed implements Runnable, Callable {
@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) mixed);
es.submit((Callable) single);
}
}Output is:
Ran Mixed
Ran Single
Called Mixed
Called SingleCode Snippets
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);
}
}Ran Mixed
Ran Single
Called Mixed
Called SingleContext
StackExchange Code Review Q#17551, answer score: 4
Revisions (0)
No revisions yet.