English 中文(简体)
one
原标题:one method classes with enum in java
  • 时间:2009-10-01 07:33:08
  •  标签:

我有一席之地。

public enum MyEnum
{
  myValue
  {
    @Override
    public String myMethod(String dostuff)
    {
      return dostuff + "One";
    }
  },
  myOtherValue
  {
    @Override
    public String myMethod(String dostuff)
    {
      return dostuff + "Two";
    }
  },
  aThirdValue
  {
    @Override
    public String myMethod(String dostuff)
    {
      return dostuff + "Three";
    }
  };

  public abstract String myMethod(String dostuff);
}

Now I think we can all agree that this looks horrible? but what would be the bether way? I could have an abstractfactory, but then i would need three implementationclasses that each as a one line method. Dont find that so pretty either. I could use a switch (either in the code or in the enum). But then i could forgett to add a case.

So, whats the way to go? There must be a pattern for this, but cant seem to find one. The best ive come up with so far is to add comments to autocollapse the methods in Netbeans, not so brilliant that either.

最佳回答

令人费解的是,解决该问题非属地延伸问题的大多数办法只是要走 around。

例如,你可以把三种不同的行为概括在三个不同的接口实施中,然后将不同的行为落实到每个平台的施工者身上。 (这基本上是其他人所建议的指挥或战略办法。)

如果你将这些执行和接口单独分类,那么你就会发现,这种行为可能超出um的范围,而 unnecessary然是没有必要的。

如果你把 en的私人静态内层带走,你将把ug线从档案顶上移至档案底。 租户的眼光要少得多。

public enum Foo {

    ONE(new OneDelegate()), 
    TWO(new TwoDelegate()),
    THREE(new ThreeDelegate());

    // ////////////////////
    // Private stuff

    private final FooDelegate delegate;

    private Foo(FooDelegate delegate) {
        this.delegate = delegate;
    }

    // ////////////////////
    // Public methods

    public String doStuff(String stuff) {
        return delegate.doStuff(stuff);
    }

    // ////////////////////
    // Helper classes

    private static interface FooDelegate {
        String doStuff(String stuff);
    }

    private static class OneDelegate implements FooDelegate {
        @Override
        public String doStuff(String stuff) {
            return "One " + stuff;
        }
    }

    private static class TwoDelegate implements FooDelegate {
        @Override
        public String doStuff(String stuff) {
            return "Two " + stuff;
        }
    }

    private static class ThreeDelegate implements FooDelegate {
        @Override
        public String doStuff(String stuff) {
            return "Three " + stuff;
        }
    }
}

另一种明显的解决办法是将所有三种行为都作为私人方法,并在公共方法中采用<条码>。 从个人角度来说,我认为这显然是罪.祸首,但很多前方案人员似乎喜欢。 <代码>:

public enum Foo {

    ONE, TWO, THREE;

    // ////////////////////
    // Public methods

    public String doStuff(String stuff) {
        switch(this) {
            case ONE:
                return doStuffOne(stuff);
            case TWO:
                return doStuffTwo(stuff);
            case THREE:
                return doStuffThree(stuff);

    // If we re handing all enum cases, we shouldn t need
    // a default (and per comments below, if we leave out
    // the default, we get the advantage that the compiler
    // will catch it if we add a new enum value but forget
    // to add the corresponding doStuff() handler

    //      default:
    //          throw new IllegalStateException("Who am I?");
        }
    }

    // ////////////////////
    // Static helpers

    private static String doStuffOne(String stuff) {
            return "One " + stuff;
    }

    private static String doStuffTwo(String stuff) {
            return "Two " + stuff;
    }

    private static String doStuffThree(String stuff) {
            return "Three " + stuff;
    }
}
问题回答

解决办法是为 en建立一个私人建筑:

public enum MyEnum
{
  myValue("One"), myOtherValue("Two"), aThirdValue("Three");

  private String value;

  private MyEnum(String value) { this.value = value; }

  public String myMethod(String dostuff)
  {
    return dostuff + value;
  }
}

[EDIT] 指出,你可以通过更为复杂的事情。 例如,你可以采用某种接口的班次(见Work,该班有doWork(>)。 这样,你就可以储存各种方法,以从事不同的工作。

查阅command patternsstrategy patterns

什么?

     public enum MyEnum {
         myValue("One"),
         myOtherValue("Two"),
         aThirdValue("Three");

        private final String postfix;
        private MyEnum(String postfix) {
          this.postfix )= postfix;
        }
        public String myMethod(String dostuff) {
          return dostuff + postfix;
        }
      }

即便你的实际足迹更为复杂,也有一些技术可以改进。 请表明您的真正需要......





相关问题
热门标签