Lets say I have a class of type Color. I want to create some child classes, Red, Green, and Blue. Then, in a completely separate class I have a List called Rainbow of type Color. I want to be able to place all types of colors (Red, Greed, Blue) into the list Rainbow that way later I can search the list Rainbow by a type of color to see if it contains one of those colors. Here is some pseudocode of what I'm wanting to accomplish:
class Color(){}
class Red extends Color(){}
class Blue extends Color(){}
class Green extends Color(){}
class JustSomeClass(){
List<Color> Rainbow;
Rainbow.add(new Red());
Rainbow.add(new Blue());
Rainbow.add(new Green());
public Color getTypeOfColor(typeOfColor){
for(Color c : Rainbow){
if(c.getType().equals(typeOfColor)){
return c;
}
}
}
}
I'm not that familiar enough with Java to know a good way to go about accomplishing this. Can someone point me in the right direction?
Colordo? Could you maybe use anenumand aswitch? Or, otherwise, a Visitor Pattern would be better.Rainbow.add(new Red());calls inside the constructor or method.Map<String,Color>?