所以我有一个包含String-field的类:
public class A {
private String type = ...
public String getType(){
return this.type;
}
public void setType(String type){
this.type = type;
}
}我还列出了所有可能的类型,有12种,将来可能会更多。
现在,我想编写一个方法,该方法获取A类的对象,并根据类中的"type“调用特定的方法。有没有比写12条(或更多)if语句更聪明的解决方案呢?
通常我会使用Visitor-pattern,但我不想创建12个新类。
编辑:
我最终创建了一个
Map<String,Function<A,String>> map = new HashMap<String,Function<A,String>>然后调用
A a;
...
map.get(a.getType).apply(a);发布于 2015-08-23 00:41:17
您应该使用enum,而不是将类型存储为“自由格式”文本值,因为您有一个定义良好的值列表。
您甚至可以通过使用抽象方法,让不同的枚举以不同的方式实现相同的方法。这将允许您完全消除容易出错的switch语句。
下面是一个显示实例值和抽象方法的示例。所显示的模式将使实现不在枚举之外,同时让编译器在添加新枚举时捕获所有使用。
public enum Type {
INTEGER("Integer") {
@Override
public void apply(Action action, A a) {
action.applyInteger(a);
}
},
STRING ("Text") {
@Override
public void apply(Action action, A a) {
action.applyString(a);
}
};
private String displayName;
private Type(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return this.displayName;
}
public abstract void apply(Action action, A a);
}
public interface Action {
public void applyInteger(A a);
public void applyString(A a);
}
public class A {
private Type type;
public Type getType(){
return this.type;
}
public void setType(Type type){
this.type = type;
}
public void apply(Action action) {
this.type.apply(action, this);
}
}当您向类型枚举添加新类型时,还会向Action接口添加一个新方法,这将强制您在接口的所有实现中实现该方法。使用switch语句,您就不会获得这样的安全性。
发布于 2015-08-23 00:25:44
如果您使用的是JDK7或更高版本,请使用接受字符串作为参数的switch,并为每个参数编写用例。
switch (type) {
case "SomeX":
yourInstance.invokeMethod();
break;
case "SomeY":
...发布于 2015-08-23 02:12:19
我猜其他答案是正确的,但通过阅读问题,我认为更直接的答案将是使用内省和惯例:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Test {
public static class A {
private String type;
public String getType(){
return this.type;
}
public void setType(String type){
this.type = type;
}
}
public static class Actions {
public void runForType1(A a) {
System.out.println("It's type 1");
}
public void runForType2(A a) {
System.out.println("It's type 2");
}
public void runForType3(A a) {
System.out.println("It's type 3");
}
}
public static class Runner {
Actions actions;
public Runner(Actions a) {
this.actions = a;
}
public void run(A a) {
try {
Method m = actions.getClass().getMethod("runFor" + a.getType(), A.class);
m.invoke(actions, a);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Runner r = new Runner(new Actions());
A type1 = new A();
type1.setType("Type1");
A type2 = new A();
type2.setType("Type2");
A type3 = new A();
type3.setType("Type3");
r.run(type1);
r.run(type2);
r.run(type3);
}
}该示例的预期输出为:
It's type 1
It's type 2
It's type 3如果约定不可行,您可以始终使用类型到方法名的映射来创建HashMap。
https://stackoverflow.com/questions/32158288
复制相似问题