-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");
2
  • 4
    class enum... that doesnt compile or make sense
    – Reimeus
    Commented Jul 17, 2013 at 5:24
  • edited the enum class please check Commented Jul 17, 2013 at 5:35

2 Answers 2

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.

1
  • 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"); Commented Jul 17, 2013 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");
  } 
}
4
  • 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. Commented Jul 17, 2013 at 5: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... Commented Jul 17, 2013 at 6:01
  • lasercats sry the enum class name is ColorsUsage not ColorPainter Commented Jul 17, 2013 at 9:58
  • can you make your example compilable? enum class confuses me as well as it confuses other people. Commented Jul 17, 2013 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.