我有以下法典:
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)
{
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(
)方法可能导致你找到一个简单的地图或基于图的解决方案。
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...