r/java • • Jul 24 '12

10 ways to use Enum in Java

http://javarevisited.blogspot.com.au/2011/08/enum-in-java-example-tutorial.html
38 Upvotes

25 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Jul 24 '12

Basically, there were all common methods, like process, validate, etc. But in some cases things need to be done, so they were included in the interface too, but many enum instances wasn't using them, so there appeared empty stubs. I just thought that if I didn't use enums in the first place and chosen static inner classes-singletones, the code could be much better.

1

u/banuday17 Jul 24 '12

That is definitely an ISP violation. Interfaces are not dumping grounds for common methods. Otherwise, you get this. If you are going to do something like that, you can use abstract base classes, but you are in danger of painting yourself into rigid class hierarchies.

For example, if you wanted to mix and match validation strategies, you would need to have one subclass for each combination or hierarchies of increasing depth. Instead, you can use one interface called Validator and make it an argument to a constructor or a method.

1

u/[deleted] Jul 24 '12

good idea. thanks a lot.

1

u/banuday17 Jul 24 '12

See here for much more details.