-2

Ignore this question

I have a subclass which extends generic super class. Here it is

BlueColorPainter extends ColorPainter<BlueColor>;
GreenColorPainter extends ColorPainter<GreenColor>;
RedColorPainter extends ColorPainter<RedColor>;

ColorPainter painter is an abstarct class which has unimplemented method paint().

I have declared enum with

class enum ColorsUsage{
    BLUE("blue","BlueColorPainter","BlueColor"),
    Green("green","GrerenColorPainter","GreenColor"),
    Red("red","RedColorPainter","RedColor");

    String colorName,colorPainterClass,colorClass;
    ColorPainters(String colorName, String colorPainterClass,String colorClass) {
        this.colorName = colorName;
        this.colorPainterClass = colorPainterClass;
        this.colorClass = colorClass;
    }
}  

When color name is passed,the method should return the appropriate colorPainter Instance. like

String color="any"; //xxx can be blue,red,green

ColorPainter<?> colorPainter;
if(color=="blue")   
colorPainter=new BlueColorPainter();
else if(color=="red")   
colorPainter=new RedColorPainter();
if(color=="green")   
colorPainter=new GreenColorPainter();

I want an implementation method which is equivalent to above if else condition.

I have written some code making use of Enum class ColorsUsage. But could not complete it.

public static ? getPainter(String color){
       for(ColorsUsage cp:ColorsUsage.values()){
         //write code to create the instance of painter class, ex:BlueColorPainter 
       }
    }

please fill the "?" and the commented line.

how to call that method.

ColorPainter<?> painter= getPainter("blue");
Sreenivas M
  • 157
  • 3
  • 13

2 Answers2

1
 public static ? getPainter(String color){
   for(ColorsUsage cp:ColorsUsage.values()){
     //write code to create the instance of painter class, ex:BlueColorPainter 
   }
 }

In above method; instead of using Generics you can return Object (i.e. super class) and then using instanceof keyword you can find the exact type of the instance in the calling method.

rai.skumar
  • 10,309
  • 6
  • 39
  • 55
  • public static Object getPainter(String color){ for(ColorsUsage cp:ColorsUsage.values()){ return Class.forName(cp.colorPainterClass); } } [b]calling the above method[b] ColorPainter painter= getPainter("blue"); – Sreenivas M Jul 17 '13 at 12:46
-1

You probably don't understand the intention of generics. Additionally you make it too complicated.

Try this, but keep in mind to add modifiers like public or private:

class ColorPainters{

  static ColorPainters BLUE = new ColorPainters("blue","BlueColorPainter","BlueColor"),
                       GREEN = new ColorPainters("green","GreenColorPainter","GreenColor"),
                       RED = new ColorPainters("red","RedColorPainter","RedColor");

  String colorName, colorPainterClass, colorClass;

  public ColorPainters(String name,String painter,String color){
      this.colorName=name;
      this.colorPainterClass= painter;
      this.colorClass=color;
 }  

  public static ColorPainters getPainter(String color){
      if(color.equals(BLUE.colorName))
          return BLUE;
      else if(color.equals(GREEN.colorName))
          return GREEN;
      else if(color.equals(RED.colorName))
          return RED;
      else
          return new ColorPainters(color, color + "ColorPainter", color + "Color");
  } 
}
MyPasswordIsLasercats
  • 1,610
  • 15
  • 24
  • It is not that i dont know generics.You probably didn't get my question. I am not asking how to implement the generic class.The colorPainterClass and colorClass in the enums shoudd be used to create instance using reflection. I dont want if/else statment to instantiate the painter class. method is Like factory method pattern. – Sreenivas M Jul 17 '13 at 05:51
  • Now I'm pretty sure that I don't understand what you want. You either try to explain your problem in a different way or you wait for others to help you. I'm still too dumb to understand you... – MyPasswordIsLasercats Jul 17 '13 at 06:01
  • lasercats sry the enum class name is ColorsUsage not ColorPainter – Sreenivas M Jul 17 '13 at 09:58
  • can you make your example compilable? `enum class` confuses me as well as it confuses other people. – MyPasswordIsLasercats Jul 17 '13 at 10:36