How can I compare objects using instanceof but Interface types (not
passing exact class name.)
public interface Component{}
public class AppMnger {
public void doWork(){
SomeComponent comp = new SomeComponent ();
AddComponentToList(comp);
}
public void AddComponentToList(Component compo){
componentList.add(compo);
}
/* Give me the component I want. */
public Component getComponent(Component comp){
for (Component component : getComponentList()) {
if (component instanceof comp) {
return component;
}
}
}
private ArrayList<Component> componentList = new ArrayList<Component>();
}
public class SomeComponent implements component {
public void MyMethod() {
appMnger.getComponent(this);
}
public void setAppMnger(AppMnger appm){
this.AppMnger = appm;
}
private AppMnger appm ;
}
In Above code AppMnger is having a list of components. Components are
communicating each other. So if one component want to know another
component instance it call the AppMnger getComponent(comp) method. But I
get an error when I use instanceof operator. I don't want each component
to want compare the list but I want to delegate that task to AppMnger
because he is the one who knows about components it created. Amy thought?
No comments:
Post a Comment