简体   繁体   English

使用枚举作为通用参数

[英]use enum as generic parameter

I have an enum day我有一个枚举day

public enum DAY { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}

I want to create a generic interface so that for each day I want to have a different implementation我想创建一个通用接口,以便每天都有不同的实现

public interface DayLogic<T>
{
  void doLogic();
}

I want <T> to be a value of DAY enum for example例如,我希望<T>成为DAY enum的值

public class WednesdayLogic implements DayLogic<DAY.WEDNESDAY>
{
   @Override
   public void doLogic()
   {}    
}

is it possible or is there any alternate flow to do so?有可能还是有任何替代流程可以这样做?

Since there are already answers to your original question, here's what you can do that should fit your use case: since an enum is just a (restricted) class , it can implement an interface , which looks like what you need:由于您的原始问题已经有了答案,因此您可以执行以下操作以适合您的用例:由于enum只是一个(受限的) class ,它可以实现一个interface ,看起来像您需要的那样:

public enum DAY implements DayLogic { 
 SUNDAY {
  public void doLogic() {
   // sunday logic implementation
  }
 }, 
 MONDAY{
  public void doLogic() {
   // monday logic implementation
  }
 }, 
 // ...
}

You can then use it like然后你可以像这样使用它

DAY day = decideDay();
day.doLogic();

If you don't like the individual values having the method (I don't), you can do something like如果您不喜欢具有该方法的单个值(我不喜欢),您可以执行类似的操作

public enum DAY implements DayLogic {
 SUNDAY(new SundayLogic()),
 MONDAY(new MondayLogic()),
 // ...
 ;

 private DayLogic logicDelegate; // or Runnable, it's the same signature

 private DAY(DayLogic logic) {
  logicDelegate = logic;
 }

 public doLogic() {
  logicDelegate.run();
 }
}

Looks like you can achieve this concept by:看起来您可以通过以下方式实现此概念:

public enum DAY
{
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

Day Logic interface and Factory initializer Day Logic 接口和工厂初始化程序

public interface DayLogic
{
    void doLogic();

    public static class Factory
    {
        public DayLogic getDayLogic(DAY day)
        {
            if (DAY.WEDNESDAY.equals(day))
            {
                return new WednesdayLogic();
            }
            // ...
            return null;
        }
    }
}

WednesdayLogic Class:星期三逻辑 Class:

public class WednesdayLogic implements DayLogic
{
    final DAY day = DAY.WEDNESDAY;

    @Override
    public void doLogic()
    {
        // TODO Auto-generated method stub

    }
}

Problem is that Enum Day is a value, not a class.问题是枚举日是一个值,而不是 class。

The answer from daniu is one option: the enum can implement an interface, and you do all in the interface.大牛的回答是一个选项:枚举可以实现一个接口,你在接口中做所有事情。 Especially using a constructor with DayLogic is nice.尤其是在 DayLogic 中使用构造函数非常好。

Often that DayLogic is a later, uncoupled phenomenon.通常,DayLogic 是一种较晚的、分离的现象。 In that case use an EnumMap from the enum to your logic.在这种情况下,使用枚举中的EnumMap到您的逻辑。

public class Dayly {
    private final Map<Day, DayLogic> logicMap = new EnumMap<>(Day.class);

    public void putLogic(Day day, DayLogic logic) {
        logicMap.put(day, logic);
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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