I have the following piece of code. It's kind of a factory function that retrieves an instance of a writer base on it's type. Notice that the type is an enum
.
public Writer getWriter(WriterTypeEnum type){
switch(type){
case A: new AWriter() break;
case B: ... break;
case C: ... break;
...
}
}
The problem is I have about 30 case
s. What can I do to reduce them or to not implement them at all?
I solved cases like this in the past using strategy pattern but here I am facing an old enum
used in the entire app. The other problem is that I cannot inject spring beans into this enum. Some of the instances from switch cases are bean
s.
The WriterTypeEnum
has an ID that is coming from UI and based on that ID I have to determine the right instance.