Sad logic on types
Posted
by
user2972231
on Stack Overflow
See other posts from Stack Overflow
or by user2972231
Published on 2013-11-09T15:44:50Z
Indexed on
2013/11/09
15:53 UTC
Read the original article
Hit count: 224
java
Code base is littered with code like this:
BaseRecord record = // some BaseRecord
switch(record.source()) {
case FOO:
return process((FooRecord)record);
case BAR:
return process((BarRecord)record);
case QUUX:
return process((QuuxRecord)record);
.
. // ~25 more cases
.
}
and then
private SomeClass process(BarRecord record) { }
private SomeClass process(FooRecord record) { }
private SomeClass process(QuuxRecord record) { }
It makes me terribly sad. Then, every time a new class is derived from BaseRecord
, we have to chase all over our code base updating these case statements and adding new process
methods. This kind of logic is repeated everywhere, I think too many to add a method for each and override in the classes. How can I improve this?
© Stack Overflow or respective owner