0

I have got a problem where I calculate a number and according to this number I have to call a specific method. I ended up with the idea of creating an enum in which each element calls another method. Just as described in this post https://stackoverflow.com/a/4280838/2426316

However, the poster of that answer also mentioned that it would not be considered a very good design, so that I am wondering what I should do. Is it safe to implement an algorithm that uses this design? If not, what else can I do?

Community
  • 1
  • 1
user2426316
  • 6,541
  • 20
  • 50
  • 81
  • 1
    What class is the number? Or is it a primitive data type? This design can be good/bad based on what and how you are trying to do this – But I'm Not A Wrapper Class Aug 12 '13 at 15:21
  • 2
    Enums do not lend themselves very well to representation of numbers, unless it is a fairly small set of known integers. Better might be to use an array of `Runnable`. Perhaps you can describe what you are trying to do in more detail. Example code (or pseudocode) would help. – Ted Hopp Aug 12 '13 at 15:21
  • why aren't yuou doing the first answer in the linked question, that is the best – NimChimpsky Aug 12 '13 at 16:15

2 Answers2

2

The Java Enum type is a language level support (syntactic sugar) for the type-safe enum pattern.

One of the advantages of the type-safe enum pattern and the Java Enum type (compared to other solutions such as C# enums) is that it's designed to support methods, even abstract ones.

Possible usage:

  • places where you would use the Strategy pattern, but have a fixed set of strategies
  • replace switch statements with polymorphism (prefered)
  • ...

For more information:

  • Effective Java, by Joshua Bloch.
    • First edition includes the type-safe enum pattern
    • Second edition includes the Java Enum type
  • Refactoring, by Martin Fowler (e.g. Replace conditional with polymorphism)
Puce
  • 36,099
  • 12
  • 75
  • 145
  • so is it safe to implement an enum in which other methods are called. I am a bit confused since the other poster and the one in the link of my question did not recommend me this approach. – user2426316 Aug 12 '13 at 15:58
  • Yes, generally it can be a very good practice. Of course not in all situations and as others already asked, it's not clear how you want to use an enum to map from numbers to methods, unless you have a small set of known numbers. – Puce Aug 12 '13 at 16:03
  • it would be like 50 different (known) numbers. – user2426316 Aug 12 '13 at 16:06
  • 1
    And 50 different methods? And if yes, isn't it possible to create a small set of methods which use the number as a parameter? – Puce Aug 12 '13 at 16:12
-3

Well, safety probably isn't the issue here. It is uncommon an can be difficult to follow though.

My recommendation would be to build around this in one of two ways:

  1. Use ENUMS but don't include the function calls in the ENUM code itself. Rather have a function explore(Level1 level){...} that has a switch statement which differentiates by the level passed.
  2. Use Lambda expressions. This is the cleaner solution as it allows you to pass functions as arguments; sadly Java won't support this method natively until Java 8 is released. There are however implementations to simulate Lambda expressions, the best known probably being lambdaj.
blalasaadri
  • 5,873
  • 5
  • 39
  • 58
  • Thanks, so would you say that it is `ok` to use a method that contains a switch statement which itself contains lots of cases which call other methods? – user2426316 Aug 12 '13 at 15:30
  • 4
    Under what theory is a `switch` statement better than having an `enum` class declare an abstract method that each `enum` constant needs to implement? – Ted Hopp Aug 12 '13 at 15:31
  • OK yes, but it's not pretty. I really would recommend lambda functions though. @TedHopp: Enums are normally used to enumerate stuff; yes, they support functions but using those functions can make reading the code very difficult. As I said above, using lambda functions would be the cleaner solution. – blalasaadri Aug 12 '13 at 15:36
  • @blalasaadri Since you said that lambda functions are not supported yet, do you think it would be possible to get them working in an Android environment? – user2426316 Aug 12 '13 at 15:39
  • I have never tried using the lambdaj library under Android, but from what I have read it should be possible. – blalasaadri Aug 12 '13 at 15:43