public interface Component{}
public class AppManager {
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() {
appManager.getComponent(SomeComponent );
}
public void setAppMnger(AppManager appm){
this.appManager = appm;
}
private AppManager appManager ;
}
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?