Enums as type discriminator anti-pattern

In Java 1.5 enums were introduced to ease handling of constant values. Enums can improve code quality, because they are types that the compiler can check. But they can also be misused and lower the code quality. In this blog I want to discuss the misuse of enums as type discriminators and show you why this kind of usage is an anti-pattern from an object-oriented perspective. Type-switches What I call a “type-switch” is just any conditional statement that does type checks in order to execute type specific code. Here are some examples of type switches. HumanState humnanState = …; if(humanState instanceof HappyState){ singHappySong(); } else if(humanState instanceof SadState){ singDirge(); } HumanState humanState =… Read More »Enums as type discriminator anti-pattern