I have a universal mapper class with a method that determines which transformer implementation should be used. The indicator for this is an enum. The method looks like the following:
private void test() throws Exception {
MyEnum nameEnum = ...
ITransformer myTransformer;
switch (nameEnum) {
case ENUM1:
mytransformer= new TransformerImpl1(this);
mytransformer.doUnmarshal();
break;
case ENUM2:
mytransformer= new TransformerImpl2(this);
mytransformer.doUnmarshal();
break;
case ENUM3:
mytransformer= new TransformerImpl3(this);
mytransformer.doUnmarshal();
break;
...// 40 x
}
}
I know about filling a static map to avoid such switch/case statements, but here I have to return an instance of a class, so I think that won't work.
I think I also can't give the information which transformer to choose to the enum itself as I have to give the MapperClass as parameter to the transformer implementation.
But I think there must be a solution to avoid such long switch/case statements, even if in my case the classes have a "not so good" coupling.