Common methods between enumerations

I want to reorganize emun in two new enums, but I don't like copy / pasting enum methods into all new enums.

enum EmailType { REMINDER_ADMIN('reminderForAdmin') REMINDER_PRODUCTION('reminderForProduction') REMINDER_MANAGEMENT('reminderForManagement') REMINDER_CUSTOMER('reminderForCustomer') private final propertiesIdentifier String getTemplate(type) { ... } String getFrom(type) { ... } String getTo(type) { ... } String getBcc(type) { ... } ... } 

Is it possible to implement methods only once and use them in several enumerations?

 enum EmailTypeAdministration { REMINDER_ADMIN('reminderForAdmin') REMINDER_PRODUCTION('reminderForProduction') ... } enum EmailTypeClients { REMINDER_MANAGEMENT('reminderForManagement') REMINDER_CUSTOMER('reminderForCustomer') ... } 
+7
source share
4 answers

Finally, the Mixin solution does not work, because the @Mixin annotation @Mixin works with classes, not enumerations.

I use a similar approach with a delegate. Delegate conversion works great! This code can improve the use of EnumUtils with a factory or singleton pattern.

 class EnumUtils { String getTemplate(type) { "template" + type } String getFrom(type) { } String getTo(type) { } String getBcc(type) { } } enum EmailTypeAdministration { REMINDER_ADMIN('reminderForAdmin'), REMINDER_PRODUCTION('reminderForProduction') @Delegate EnumUtils enumUtils = new EnumUtils() EmailTypeAdministration(str) {} } enum EmailTypeClients { REMINDER_MANAGEMENT('reminderForManagement'), REMINDER_CUSTOMER('reminderForCustomer') @Delegate EnumUtils enumUtils = new EnumUtils() EmailTypeClients(str) {} } EmailTypeAdministration emailTypeAdmin = EmailTypeAdministration.REMINDER_ADMIN assert 'templateParam' == emailTypeAdmin.getTemplate('Param') 
+2
source

As my old friend Clippy would say, "it looks like you're using Groovy." If so, you can use mixin to add common methods for both enumerations.

 // This class holds the methods that will be mixed-in to the enums class EnumUtils { String getTemplate(type) { "template" + type } String getFrom(type) { } String getTo(type) { } String getBcc(type) { } } // This annotation adds the common methods to the enum @Mixin(EnumUtils) enum EmailTypeAdministration { REMINDER_ADMIN('reminderForAdmin'), REMINDER_PRODUCTION('reminderForProduction') EmailTypeAdministration(str) {} } // This annotation adds the common methods to the enum @Mixin(EnumUtils) enum EmailTypeClients { REMINDER_MANAGEMENT('reminderForManagement'), REMINDER_CUSTOMER('reminderForCustomer') EmailTypeClients(str) {} } // Quick test to prove the methods exist and return the expected values EmailTypeAdministration emailTypeAdmin = EmailTypeAdministration.REMINDER_ADMIN assert 'templateParam' == emailTypeAdmin.getTemplate('Param') 

You can run the code above in the Groovy console to prove that it works as advertised

+9
source

Enums cannot extend any other class, since all enumerations automatically extend the class named Enum . Thus, your only option is to delegate the implementation of methods for a separate utility. This can make a difference if the implementation is not trivial (more than one line). Otherwise, delegation does not give you serious advantages.

Another possibility is to manually expand Enum, but I'm ready to write detailed code like valueOf() , values() , etc., so I'm not sure if you really need it.

EDIT:

Take a look at my article on Hierarchical Enumerations . Perhaps this will help you too.

+2
source

The Enum type cannot do this, but you can use the Groovy Mixin or factory with an interface:

  • In the enumerations, just define the constants. All enumerations must implement a common token interface.
  • Create a factory that accepts a marker interface and contains getters.

The factory approach allows you to move the configuration (for example, templates, email addresses) to the configuration file that the factory reads at startup.

Lesson: Do not put the configuration in enumerations. Enumerations are constants. Configuration changes.

0
source

All Articles