Extensible Enum With Interface
Everyone knows you can't extend Enums, right? Well, with some coding magic, it turns out how can. See how to make Enums extensible with Interface's help.
Join the DZone community and get the full member experience.
Join For FreeIn Java, Enum is a powerful data type. There are a lot of places you can use Enum to take advantage of it, like in a Static Factory method. There, it's a wise decision to pass the Enum type rather than passing a String. Below, I mention some situations where using Enum is advantageous.
Enum Advantages
- In a Static Factory method, passing Enum as an argument makes the method typesafe. Method users can’t pass an arbitrary value to this method.
- Instead of using bit fields, using Enum and EnumSet makes the code cleaner and easy to maintain.
- Using Enum can achieve a Singleton pattern, which is inherently Thread Safe.
- You can also use them to get rid of If/Switch statements.
Although Enum has several advantages, it has one major disadvantage. We cannot extend Enum.
So the Question is, “Can we extend Enum, and ehen it is needed?”
Yes we can emulate the extensibility in Enum, but we have to do it in a strategic way.
We know that Enum can’t be extended, but we know the fact that an Interface can be implemented and Enum can implement the Interface. So, combining these two statements, we can achieve extensibility. Where Enum can’t be extended, if we use the “Coding to an Interface” Strategy, we can easily replace BaseType Enum with our Extended Enum.
Now coming to the use case:
According to Joshua Bloch, “There is at least one compelling use case for extensible enumerated types, which is operation codes, also known as opcodes. An opcode is an enumerated type whose elements represent operations on some machine, such as the Operation. Sometimes, it is desirable to let the users of an API provide their own operations, effectively extending the set of operations provided by the API.”
According to Josh Bloch, in the case of an operation, we can provide some basic operations in an Enum — like plus/minus in a simple calculator — but if API users want more, like a square root operation, they can achieve it through extending the Operation Interface and create a new Enum.
Steps For Achieving Extensibility
Let’s take a use case where we have an Enum called Direction in an API. It has entries for NORTH, SOUTH, EAST, and WEST. In most cases, API users use these basic directions, but when it is about to show the shortest path between the source and the destination, API users need some advanced direction, like NORTH-EAST, NORTH-WEST, etc.
How can we judiciously implement the API so users can have the option to extend the Basic Direction Enum?
Steps
- Create an Interface called Direction and declare a method called showDirection().
- Create a BasicDirection Enum by implementing Direction.
- Put entries for NORTH, SOUTH, EAST, and WEST.
- Please note that as it implements the Direction interface, each Enum has to override the showDirection method.
- Create an AdvanceDirection Enum with NORTH-EAST, NORTH-WEST, etc. entries.
- Use this Custom Enum/Basic Enum anywhere interchangeably.
Java Code
Direction Interface
package com.example.enumtest;
public interface Direction {
public void showDirection();
}
BasicDirection Enum
package com.example.enumtest;
public enum BasicDirection implements Direction {
NORTH {
@Override
public void showDirection() {
System.out.println("I am NORTH");
}
},
SOUTH {
@Override
public void showDirection() {
System.out.println("I am South");
}
},
EAST {
@Override
public void showDirection() {
System.out.println("I am EAST");
}
},
WEST {
@Override
public void showDirection() {
System.out.println("I am WEST");
}
}
}
AdvanceDirection Enum
package com.example.enumtest;
public enum AdvanceDirection implements Direction {
NORTHEAST {
@Override
public void showDirection() {
System.out.println("I am NORTH-EAST");
}
},
NORTHWEST {
@Override
public void showDirection() {
System.out.println("I am NORTH-WEST");
}
},
SOUTHEAST {
@Override
public void showDirection() {
System.out.println("I am SOUTH-EAST");
}
},
SOUTHWEST {
@Override
public void showDirection() {
System.out.println("I am SOUTH-WEST");
}
}
}
DirectionDriver Class
package com.example.enumtest;
public class DirectionDriver {
public static void printDirection(Direction op)
{
op.showDirection();
}
public static <T extends Enum<T> & Direction> void printDirections(Class<T> clazz)
{
for(Direction direction : clazz.getEnumConstants())
{
direction.showDirection();
}
}
public static void main(String[] args) {
DirectionDriver.printDirection(BasicDirection.EAST);
DirectionDriver.printDirection(AdvanceDirection.SOUTHWEST);
DirectionDriver.printDirections(BasicDirection.class);
DirectionDriver.printDirections(AdvanceDirection.class);
}
}
Output
I am EAST
I am SOUTH-WEST
I am NORTH
I am South
I am EAST
I am WEST
I am NORTH-EAST
I am NORTH-WEST
I am SOUTH-EAST
I am SOUTH-WEST
Please pay attention to the class DirectionDriver, where I create two methods
printDirection
, which takes Direction as the input, and I pass an Enum entry to it.Another version is that the printDirection method prints all Enum entries. To achieve that, I make a generic type that says T must be an Enum and a subtype of Direction by <T extends Enum<T> & Direction>, then pass the class instance so I can iterate over EnumConstants.
Published at DZone with permission of Shamik Mitra, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments