-1

In the below example Enums do the amount of processing that a class would do.

enum TriggerHandlerType {
    DASHBOARD {
        @Override
        TriggerHandler create() {
            return new DashboardTriggerHandler();
        }
    },
    COMPONENT_HANDLER {
        //...
    };

    abstract TriggerHandler create();
}

private static TriggerContext getTriggerContext(TriggerHandlerType triggerHandlerType) throws TriggerHandlerException {
    return new TriggerContext(triggerHandlerType.create());
}

Enums are usually used for type safe storage of constants where as in this case they will be returning varying values based on the processing logic. In a way its seems to be a comprehensive technique as the Enums here do the state determination themselves which eases the processing of classes. Also since the return values are a subset of finite values, it seems to make some sense to have the processing handled by the Enums themselves.

I do see problem here where this will break the Open-Close principle in SOLID and the class will have increment in lines of code whenever more enums get added, Could anyone share your thoughts on this?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Keshan Nageswaran
  • 8,060
  • 3
  • 28
  • 45
  • 1
    It depends on the context, in this case, it an awful idea. – David Pérez Cabrera Sep 15 '16 at 08:09
  • @DavidPérezCabrera Thanks for the comment i saw this code in Github and thought it will really ease the implementation than having different concrete classes or switch cases or hashmaps, why do you think its awful! any specific reason – Keshan Nageswaran Sep 15 '16 at 08:11
  • @RealSkeptic I agree!, I took that from github, i removed it and created one just take a look hope it makes sense now – Keshan Nageswaran Sep 15 '16 at 08:17
  • 4
    Enums are classes. They are meant to be much more than just a typesafe alternative to int constants. Open-Close depends on other implementation details and not on whether you use enums or not. Downside of enums is that you can't extend (subclass) them so they are kind of closed/closed. – zapl Sep 15 '16 at 08:25
  • @zapl Thank you, that makes sense! – Keshan Nageswaran Sep 15 '16 at 08:34
  • @DavidPérezCabrera I am also intersted in why you think this to be an awful idea. A bit more context on a comment like that would make it more useful. A similar example was even mentioned in the Java Magazine July/August - "Polymorphic Dispatch With enums" – pandaadb Sep 15 '16 at 09:06
  • @DavidPérezCabrera right :) sorry i didn't see the original post; I think I am missing that bit of info. Edit: Found out how to see previous revision - I get it now :) Sorry about the confusion – pandaadb Sep 15 '16 at 09:33
  • 1
    _Could anyone share your thoughts on this?_ is not an appropriate question for StackOverflow. This is not a discussion forum. Please ask a question which can be answered objectively. See [how to ask](http://stackoverflow.com/help/how-to-ask) and [what to avoid](http://stackoverflow.com/help/dont-ask). – jaco0646 Sep 15 '16 at 12:59
  • @zapl: since `enum`s can implement `interface`s, you can have both, a fixed set of stateless built-in implementations *and* extensibility, if you care to use the `interface` rather than the `enum` type at the appropriate places. – Holger Sep 15 '16 at 14:45

2 Answers2

0

the real answer is: depends what is the enum defined for..

you can define enum that do some functionality constantly implemented by your API clients as syntax sugar...

a good example of that is the java TimerUnit where every constant is a final class that can be used to calculate time transformations

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html#toHours(long)

other enums implement the comparator interface and implement in its constants already defined sort criterias..

all of them are valid since are sintax sugar

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

I had an enum as such, doing operations like OR, AND, SEQ and such.

With java 8 and just one overriden method you could also make a constructor with a functional interface as parameter.

enum TriggerHandlerType {
    DASHBOARD(() -> DashboardTriggerHandler::new)),
    COMPONENT_HANDLER (() -> { ... });

    private final Fun fun;

    private TriggerHandlerType(Fun fun) {
        this.fun = fun;
    }

    public TriggerHandler create() {
        fun.apply();
    }
}

In an other case I did not use this technique, to decouple classes, and have clear tiers of classes. The enum was an early class not already using later classes.

A Map from enum to handler would be OO too. A unit test might check that the created map has a size equal to the enum values'.

I need not say, that enum is an artificial coupling. Fixed number of elements or not, one could make separate classes/singletons.

so it depends.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138