I have an example like this
class A {
A() {}
public C createC () {
...
}
}
class B {
B() {}
public C createC () {
}
}
Objects for A and B are created based an an
public enum D { ii, jj };
And I see code all over the place like
D d;
switch (d) {
case ii: (new A()).createC(); break;
case jj: (new B()).createC(); break;
};
How can I avoid the switch cases all over the place? I understand the code is not so clear.
I would be inclined to add the create code to the enum.
You can then replace the switch with
Having said that I would also look at making the createC method static or moving the createC code to the enum rather than leaving it in the A and B classes.
If you make the createC method in A and B static the enum would look like