Issue
Is there a way to bind two implementations to a single interface and trigger the methods of both implementaions simultaneously with interface method call. I want to use two apps for analytics and it would be ideal to trigger them both with one interface
Solution
An easy solution is to use a delegate implementation. For an interface and two implementation like this
public interface MyInterface{
public void myMethode();
}
public class MyInterfaceImplA{
public void myMethode(){
return ;
}
}
public class MyInterfaceImplB{
public void myMethode(){
return ;
}
}
You can do a third implementation
public class MyInterfaceDelegate{
private List<MyInterface> myInterfaceList;
public MyInterfaceDelegate(List<MyInterface> myInterfaceList){
this.myInterfaceList = myInterfaceList;
}
public void myMethode(){
myInterfaceList.forEach(MyInterface::myMethode)
}
}
And inject it like that (Or with @Inject) :
@Provided
MyInterfaceImplA implA(){
return new MyInterfaceImplA();
}
@Provided
MyInterfaceImplB implB(){
return new MyInterfaceImplB();
}
@Provided
MyInterface interface(MyInterfaceImplA myInterfaceImplA,MyInterfaceImplB myInterfaceImplB){
return new MyInterfaceDelegate(Lists.newArrayList(myInterfaceImplA, myInterfaceImplB));
}
Answered By - Ruokki
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.