java - Problems with Interfaces and FactoryPattern -


i have created factory pattern create objects, each of objects different , want them implement different methods, current setup have declare methods in interface , in subsequent object classes, how can around this?

this example of structure

food class

public class food extends myobjects implements myactiveobject, {  public food(string name, coordinates coordinates, int size, color color) {     super(name, coordinates, size, color);       } } 

herbivore class

public class herbivore extends entity implements myactiveobject { public herbivore(string name,coordinates coordinates, int size, color color){     super(name, coordinates, size, color); } 

}

interface

public interface myactiveobject extends entityinterface {      public coordinates getcoordinates();     etc... } 

factory class

public class myactiveobjectfactory {  public myactiveobject createobject(string objecttype, int objectcount, int x, int y, int scale){      if(objecttype == null){          return null;     }else{          if(objecttype.equalsignorecase("herbivore")){             return new herbivore();         }         else if(objecttype.equalsignorecase("carnivore")){             return new carnivore();         }         else if(objecttype.equalsignorecase("food")){             return new food();         }         else if(objecttype.equalsignorecase("drink")){             return new drink();         }        }     return null; } 

object creation

myactiveobject activeobject = activeobjectfactory.createobject("herbivore", activeobjectcount, x, y, scale); 

ive removed unessary code save space, im trying store food , herbivore activeobject in array want able call different methods on each of them, cannot change them abstract class due inheritence im out of ideas, can help?

thank in advance

perhaps i'm misunderstanding question, why can't have this? getcoordinates method defined in interface , implemented in each class, each class has own methods.

public class test{     private myactiveobject[] mobjlist = new myactiveobject[ 4 ];      public test(){         mobjlist[0] = new food();         mobjlist[1] = new drink();         mobjlist[2] = new herbivore();         mobjlist[3] = new carnivore();     }      public void dotest(){         for( myactiveobject obj : mobjlist ){             obj.getcoordinates();  // no conversion required common method              if( obj instanceof herbivore ){                 herbivore newobj = ( herbivore ) obj;                 newobj.doherbivorework();             }             else if( obj instanceof carnivore ){                 carnivore newobj = ( carnivore ) obj;                 newobj.docarnivorework();             }             else if( obj instanceof food ){                 food newobj = ( food ) obj;                 newobj.dofoodwork();             }             else if( obj instanceof drink ){                 drink newobj = ( drink ) obj;                 newobj.dodrinkwork();             }         }     }      public static void main( string[] args ){         new test().dotest();     } } 

the do<class>word methods contain print statement getcoordinates methods. output of running above is:

getting food coordinates... working on foods. getting drink coordinates... working on drinks. getting herbivore coordinates... working on herbivores. getting carnivore coordinates... working on carnivores. 

update: here definition of interface:

public interface myactiveobject{     public coordinates getcoordinates(); } 

the classes entity , myobjects empty. drink , food extend myobjects:

public class drink extends myobjects implements myactiveobject{     @override     public coordinates getcoordinates(){         system.out.println( "getting drink coordinates..." );         return new coordinates();     }     public void dodrinkwork(){         system.out.println( "working on drinks." );     } } 

carnivore , herbivore extend entity:

public class carnivore extends entity implements myactiveobject{     @override     public coordinates getcoordinates(){         system.out.println( "getting carnivore coordinates..." );         return new coordinates();     }     public void docarnivorework(){         system.out.println( "working on carnivores." );     } } 

update second: think see problem. interface using has lot of method signatures not needed in classes. it?

in case, create empty marker interface purpose of being able place different classes in array.

public interface arrayable{}; 

Popular posts from this blog