Implementing composite in Java using Generics/Interface -
i have simple example:
public class imap { private responder responder; public imap(){ this.responder = new responder(this); } public void startservice(){ responder.dostuff(); } public boolean supportsme(){ return true; } } public class udp { private responder responder; public udp(){ this.responder = new responder(this); } public void startservice(){ responder.dostuff(); } public boolean supportsme(){ return false; } } public class responder { object parent; public responder(object parent){ this.parent = parent; } public void dostuff(){ if(parent.supportsme()){ system.out.println("parent supports me."); } else { system.out.println("parent doesn't support me."); } } }
the problem supportsme undefined object. however, defined both udp , imap. responder intelligent enough call right object. believe solution use generics interface. tried following generic still similar compile error (the method supportsme() undefined type t):
public class responder<t> { t parent; public responder(t parent){ this.parent = parent; } public void dostuff(){ if(parent.supportsme()){ system.out.println("parent supports me."); } else { system.out.println("parent doesn't support me."); } } }
any solutions problem?
Comments
Post a Comment