PrabaharanKathiresan
PrabaharanKathiresan

Reputation: 1129

enums to do the computation

Can we pass objects to enum abstract methods and do a computation in enums? Here is a scenario, I have four constants and each one have their own value assigned to it. Based on the constants I will do the computation in my method.. Instead I would like to do the computation in enum and would like to get the response. To do the computation in enum I have to pass two/three reference objects to the enum methods...

ex: Consider school as enum, constants are TEACHER(LOWLEVELACCESS), STUDENT(NOACCESS), OFFICEADMIN(OFFICEACCESS).

In enum, I have abstract method process which is receiving USER POJO , strings as arguments and update some fields in the object and return the same USER POJO (with updated) to the caller. By this I can sync up the constants and their logic in enum itself.

So my question,

  1. Is it alright to have this implementation in enum? (I have seen most of the examples treat enums to store constant values not using them for any computation)

  2. This computation can be done by using methods in classes, what is the benefit if I do the computation in enum methods?

  3. Is it possible to create getter/setter method in enum?

Thanks In Advance!!

Upvotes: 1

Views: 134

Answers (3)

davidxxx
davidxxx

Reputation: 131436

Is it alright to have this implementation in enum?

It is a design choice.
Enum brings some advdantanges to provide service operations.
Enum values are singletons out of the box, these are self explanatory, there are memory efficient, etc... but have also some limitations, you cannot directly derive from an enum, so you should introduce an interface behind the enum if you want to be able to test your code and avoid coupling the client classes that do the computation with the enum ... if later you change your mind about the enum usage.

This computation can be done by using methods in classes, what is the benefit if I do the computation in enum methods?

You reason in terms of objects. You don't need to create a service and indirection in the code since the enum that is a domain object does the computation.
The enum values and the processings associated make part of the same concern. So, gathering them is not necessary a bad smell.
Nevertheless, be aware if you start to write a lot of processing that do very different things in the enum methods, you should probably get them out the enum.
This is a bad smell as the enum should not become a god object.

Is it possible to create getter/setter method in enum?

Providing data, yes but setting data of the enum : no, you must not.
Otherwise you build a stateful service and you risk to finish with synchronization concerns.

Upvotes: 4

Stefan Warminski
Stefan Warminski

Reputation: 1835

Enums are defined to be final. Computations are allowed as far as the result is equal for the same input. You also can modify the input instance, but you should not define any setter in your enum due the enum is not immutable in that case.

See also Example 8.9.2-4 here

Upvotes: 0

OldCurmudgeon
OldCurmudgeon

Reputation: 65831

IMHO:

  1. Is it alright to have this implementation in enum?

Yes - I do it all the time.

enum Ops implements Op {
    Nop{
        @Override
        public int filter(int old, int now) {
            // Unchanged.
            return now;
        }
    },
    Diff{
        @Override
        public int filter(int old, int now) {
            return a(now)
                    | (Colour.MidGrey + (r(now) - r(old))) << 16
                    | (Colour.MidGrey + (g(now) - g(old))) << 8
                    | (Colour.MidGrey + (b(now) - b(old)));
        }
    };
}
  1. This computation can be done by using methods in classes, what is the benefit if I do the computation in enum methods?

The code for the functionality is in one place. This is always a good thing.

  1. Is it possible to create getter/setter method in enum?

Yes - but don't. Remember that there is one instance of each enum for the lifetime of your code. Adding/removing functionality on a global object is very likely to hurt you later.

Upvotes: 5

Related Questions