简体   繁体   中英

Using Non-Static Methods as Enum Properties with Generics

I'll try to keep this short. I'm trying to do something like this:

public enum Fruit {
    APPLE("Apple", appleHelper::doAppleThing),
    ORANGE("Orange", orangeHelper::doOrangeThing);

    private String name;
    private Function<String, List<T>> fruitFunction;

    Fruit(String name, Function<String, List<T>> fruitFunction) {
      this.name = name;
      this.fruitFunction = fruitFunction;
    }

    public String getName() {
      return name;
    }

    public <T> List<T> applyFruitFunction(String someString) {
      return fruitFunction.apply(someString);
    }
}

Such that later, I can have a method like

private <T> List<T> doFruitThing(String someString, Fruit fruit) {
    List<T> transformedFruits = fruit.applyFruitFunction(someString);

    if (transformedFruits.isEmpty()) {
        throw new FruitException("There was no fruit of type " + fruit.getName());
    }

    return transformedFruits;
}

There's two problems I'm running into here.

  1. doAppleThing and doOrangeThing are not static methods, and ideally will stay that way, and I can't find any way of creating a local instance of appleHelper and orangeHelper to make the method reference work.
  2. Even if I were to make the methods static, enums can't have Type parameters, so there's no way to have Function<String, List<T>> fruitFunction as a field.

Is there a way this can be done? Or a better approach to this?

Enum values can have their own method implementations. So I would write this as:

public enum Fruit {
    APPLE("Apple") {
        private final AppleHelper helper = new AppleHelper();

        @Override
        public <T> List<T> applyFruitFunction(String someString) {
            return helper.doAppleThing(someString);
        }
    },

    ORANGE("Orange") {
        private final OrangeHelper helper = new OrangeHelper();

        @Override
        public <T> List<T> applyFruitFunction(String someString) {
            return helper.doOrangeThing(someString);
        }
    };

    private String name;

    Fruit(String name) {
      this.name = name;
    }

    public String getName() {
      return name;
    }

    public abstract <T> List<T> applyFruitFunction(String someString);
}

However, if you get to the point of needing per-instance state for enum instances, the thing you have is less and less an enum and more of just an abstract base class. It might be better to look into a more OO approach, using a factory/flywheel pattern for example, rather than being tied to a pure enum for this sort of thing. (It's hard to tell for sure because the code in the question is obviously just a simplified example.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM