English 中文(简体)
减少在 Java的“静态”
原标题:Reducing if-else statements in Java

我有以下法典:

void f(String t)
{
  if(t.equals("a"))
  {
    someObject.setType(ObjectType.TYPE_A);
  }
 else if(t.equals("b"))
  {
    someObject.setType(ObjectType.TYPE_B);
  }

// 50 more similar code

}

是否有简单的方式来改写“如果是”条件,以免有这么多的法典?

最佳回答

您应利用一些东西来消除<代码>、正文、正辛佩(......)的重复。 参看你喜欢这种解决办法:

void f(String t) { someObject.setType(ObjectType.byName(t)); }

enum ObjectType {
  TYPE_A, TYPE_B;
  public static ObjectType byName(String name) {
    return valueOf("TYPE_" + name.toUpperCase());
  }
}
问题回答

Use a .Map 页: 1 价值观。

我补充说,这可以作为论坛的一个功能:

public enum ObjectType {
    TYPE_A("a"),
    TYPE_B("b");

    private String stringType;

    private ObjectType(String stringType) {
        this.stringType = stringType;
    }

    public String getStringType() {
        return this.stringType;
    }

    public static ObjectType fromStringType(String s) {
        for (ObjectType type : ObjectType.values()) {
            if (type.stringType.equals(s)) {
                return type;
            }
        }
        throw new IllegalArgumentException("No ObjectType with stringType " + s);
    }
}

...

void f(String t) {
    someObject.setType(ObjectType.fromStringType(t));
}

如果您能够将<条码>t改为char,你可使用<条码>switch代替(Java 6):

void f(char t) {

  switch(t) {

    case  a`:
      someObject.setType(ObjectType.TYPE_A);
      break;
    case  b :
      someObject.setType(ObjectType.TYPE_B);
      break;

    // ...

  }

}

正如Marko所指出的,你也可以在 Java7处使用<编码>String。

这个数字要短得多,但还要高。 此外,我认为,正如<条码>switch<>/条码>,它可能也更快。 工作范围接近O(1) (有些人确认这是否真的?),有的<代码>if 说明是O(n)

更复杂的执行,而不仅仅是一个<代码>setType 您可考虑State patterns 执行。

页: 1 如果条件超过3,你可以调换发言。

http://en.wikipedia.org

其它建议是巨大的,特别是mar和地图。 但是,我在此讨论的第一个最基本因素是找到一种方法,直接归还遗体,使电梯只能够以这种方法换取回报价值。

void f(String t) {
  final ObjectType type = findType(t);
  if (type != null)
    someObject.setType(type);
  }

ObjectType findType(String t) {
  if (t.equals("a")) return ObjectType.TYPE_A;
  if (t.equals("b")) return ObjectType.TYPE_B;
  // 50 more similar code
  }

在某些情况下,这本身就足够了;在另一些情况下,findType()方法可能导致你找到一个简单的地图或基于图的解决方案。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签