A Java method class with enum

I have an enumeration that looks like

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 all agree that it looks bad? But what's the better way? I can have an AbstractFactory, but then I need three implementation classes, each as a line of methods Don't feel so beautiful I can use switches (in code or enumeration) But then I forgot to add a case

So, what is the last way? There must be a pattern, but it seems that one cannot be found So far, the best opinion put forward by the Hong Kong Institute of professional education is to add comments. The method of automatic collapse in NetBeans is not so wonderful

Solution

This is ugly, but most of the non trivial extensions to solve this problem will only change ugly things

For example, you can encapsulate three different behaviors in three different implementations of an interface, and then pass different behavior implementations to the constructor of each enumeration (this is basically an order or strategic approach suggested by others)

If you implement these and the interface is a separate class, you may expose the behavior outside the enumeration, which is unnecessary and ugly

If you use them as private static inner classes for enumeration, you will move the ugly from the top of the file to the bottom of the file In the eyes of onlookers, how little ugly it is

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;
        }
    }
}

Another obvious solution is to put all three behaviors in private methods and put a switch in public methods Personally, I think it's a sin, but many former C programmers seem to like it

The above is the whole content of a Java method class with enum collected and sorted out by the programming home for you. I hope this article can help you solve the program development problems encountered by a Java method class with enum.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>