【问题标题】:How can I mimic a multi-tiered menuing system using Java enums?如何使用 Java 枚举模仿多层菜单系统?
【发布时间】:2011-04-29 08:32:43
【问题描述】:

我需要完成以下(这是一个简化版本):

enum Animals{
 enum Cats{tabby("some value"), siamese("some value")},
 enum Dogs{poodle("some value"), dachsund("some value")},
 enum Birds{canary("some value"), parrot("some value")}

 private String someValue = "";

 private ShopByCategory(String someValue)
 {
     this.someValue = someValue;
 }

 public String getSomeValue()
 {
     return this.someValue;
 }
}

这样我就可以按如下方式访问这些项目:

string cat1 = Animals.Cats.tabby.getSomeValue;
string dog1 = Animals.Dogs.dachsund.getSomeValue;
string bird1 = Animals.Birds.canary.getSomeValue;

我尝试使用枚举执行此操作的原因是,我需要能够访问每一层,而不必 a) 实例化一个类,b) 将层的名称隐藏在方法名称后面,或者c) 使用迭代器遍历 EnumSet。

这有可能吗?你会建议什么而不是枚举?

【问题讨论】:

    标签: java enums n-tier-architecture enumset


    【解决方案1】:

    这是我最终实施解决方案的方式:

        public static class Animals()
        {
          public enum Cats()
          {
            tabby("some value"),
            siamese("some value");
    
            private String someValue = "";
    
            private ShopByCategory(String someValue)
            {
              this.someValue = someValue;
            }
    
            public String getSomeValue()
            {
              return this.someValue;
            }
          }
    
          public enum Dogs()
          {
            poodle("some value"),
            dachsund("some value");
    
            private String someValue = "";
    
            private ShopByCategory(String someValue)
            {
              this.someValue = someValue;
            }
    
            public String getSomeValue()
            {
              return this.someValue;
            }
          }
    
          public enum Birds()
          {
            canary("some value"),
            parrot("some value");
    
            private String someValue = "";
    
            private ShopByCategory(String someValue)
            {
              this.someValue = someValue;
            }
    
            public String getSomeValue()
            {
              return this.someValue;
            }
          }
    

    这样,我不必实例化类或调用任何特定于类的方法来获取我想要的信息。我可以像这样得到所有“一些价值”的字符串:

    string cat1 = Animals.Cats.tabby.getSomeValue;
    string dog1 = Animals.Dogs.dachsund.getSomeValue;
    string bird1 = Animals.Birds.canary.getSomeValue;
    

    【讨论】:

      【解决方案2】:
      //Animals.java
      public class Animals {
          public static class Cats {
              public static final String tabby = "some value";
              public static final String siamese = "some value";
          }
          public static class Dogs {
              public static final String poodle = "some value";
              public static final String dachsund = "some value";
          }
          public static class Birds {
              public static final String canary = "some value";
              public static final String parrot = "some value";
          }
      }
      
      //ShopByCategory.java
      public class ShopByCategory {
          private String someValue;
          public ShopByCategory(String value){
               this.someValue = value;
          }
          public String getSomeValue(){
              return this.someValue;
          }
      }
      //Main.java - an example of what you can do
      public class Main {
          public static void main(String[] args){
               ShopByCategory sbc = new ShopByCategory(Animals.Birds.canary);
               System.out.println(sbc.getSomeValue());
               System.out.println(Animals.Dogs.poodle);
          }
      }
      

      【讨论】:

      • 有趣的是看到 pre java 1.5 代码。当枚举更容易更不用说类型安全时,为什么要这样做?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 2016-11-18
      • 1970-01-01
      • 2012-09-08
      • 2014-05-15
      相关资源
      最近更新 更多