• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface vs Enum -- Which is preferable in this situation?  RSS feed

 
Christopher Schneider
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been thinking a bit about the use of enumerations versus an interface, and when one or the other is preferable. I'm looking at this from a has-a perspective.

In this hypothetical situation, I have a Delivery class. The Delivery has a method by which it is delivered. Let's say the delivery methods are Ground and Air. At this point, nothing differentiates Ground from Air delivery and they're essentially just tags. This is all the specification calls for.

The way I see it, I have two options (If there's a third I don't see, I'd like to hear that, too):

1. Have an interface, something like DeliveryType, and have Ground and Air implement that interface.
2. Have an enum containing Ground and Air.


Assuming there's not a third, better, option, I would most definitely lean toward option #1. If I want to add another new delivery type(e.g. SpaceShip or Boat), I make a new class and implement the interface. If I want to add functionality specific to a single delivery type, I can do that as well even though there is currently no unique functionality.

My reasoning is that unless I know for sure that something is never going to change, I'm not going to use an enum. Even though the refactor with an enum would be easy, I find zero refactoring preferable to easy refactoring.

Thoughts?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Schneider wrote:Assuming there's not a third, better, option, I would most definitely lean toward option #1.

From what you describe, especially this:
Christopher Schneider wrote:At this point, nothing differentiates Ground from Air delivery and they're essentially just tags.

I think an enum would be more suitable - that's exactly what enum is meant for.

If you'd want to add another delivery type, all you'd have to do is add a new member to the enum, which is a lot less work than adding a new class which implements an interface.

If the delivery types are nothing more than tags, then what would you put in the DeliveryType interface - would it just be an empty interface (a marker interface)? I regard marker interfaces as a code smell - a sign that there's probably a better way to design the code.

Note that even if the delivery type would be more than just a tag, then you could still use an enum - an enum can also have methods, which can even be overridden for each of the enum
constants.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I prefer simplicity over flexibility so I would also go with the enum in this situation. Besides, the lack of functionality/behavior in DeliveryMethod makes it difficult to justify the use of an interface.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!