I'm pretty new to Java programming and I've got a design question. At the moment what I got is the following:
public class MyFactory {
private MyFactory(){
//hidden constructor
}
public static ImageFilter getInstance(String filterType){
if(filterType == “foo“){
return new FooFilter();
}
return null;
}
}
public abstract class ImageFilter {
public abstract Bitmap filterImage(byte[] data);
//some other stuff
}
public class FooFilter extends ImageFilter {
public C filterImage(byte[] data){
//want to apply filterImageA or filterImageB depending what I put in
//at (*) and (**)
}
private A filterImageA(byte[] data){
//
}
private B filterImageB(byte[] data){
//
}
}
void main(byte[] data) {
ImageFilter bar = MyFactory.getInstance(“foo“);
BitmapType1 myBitmap = bar.filterImage(byte[] data); //(*)
BitmapType2 myBitmap2 = bar.filterImage(byte[] data); //(**)
}
In the main method I know what the resulting type is. If it is BitmapType1 I have to apply filterImageA. If it is BitmapType2 then I have to use filterImageB. Is there a generic way to do that? I read about generics, but have no idea on how to use them in this particular case. I hope thats not too confusing. Perhaps the whole approach is crap. Feel free to suggest a better one!