Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果我有数百个行动,我应该使用策略模式吗_Java_Design Patterns_Reflection - Fatal编程技术网

Java 如果我有数百个行动,我应该使用策略模式吗

Java 如果我有数百个行动,我应该使用策略模式吗,java,design-patterns,reflection,Java,Design Patterns,Reflection,我有一个班在做翻译工作。但它有数百种具体的翻译方法!操作代码决定将使用哪种方法!我想使用策略模式,但它将创建数百个子类!我想将这些方法命名为动作代码的末尾,并使用反射来进行转换,但我担心的是中止执行性能。它将被频繁调用!我应该使用什么设计模式来解决这个问题! 代码如下: public class Test003_Translate { private static final String PREFIX = "translate"; public static v

我有一个班在做翻译工作。但它有数百种具体的翻译方法!操作代码决定将使用哪种方法!我想使用策略模式,但它将创建数百个子类!我想将这些方法命名为动作代码的末尾,并使用反射来进行转换,但我担心的是中止执行性能。它将被频繁调用!我应该使用什么设计模式来解决这个问题! 代码如下:

public class Test003_Translate {

private static final String PREFIX = "translate";

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Test003_Translate translate = new Test003_Translate();
    Map<String, String> map = new HashMap<>();
    map.put("key001", "001");
    map.put("key002", "002");
    map.put("key003", "003");
    translate.doTranslate(map, "key001");
}

private void doTranslate(Map<String, String> map, String key) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    String actionCode = map.get(key);
    Method method = Test003_Translate.class.getMethod(PREFIX + actionCode, String.class);
    String arg = "arg: ";
    Object s = method.invoke(this, arg);
}

public String translate001(String input){
    return input + "001";
}
public String translate002(String input){
    return input + "002";
}
public String translate003(String input){
    return input + "003";
}
}
enum Key {
    KEY_001,
    ....
}

EnumMap<Key, Runnable> enumMap = new EnumMap<>(Key.class);
enumMap.put(Key.KEY_001, YourClass::translate001);
....
公共类Test003\u翻译{
私有静态最终字符串前缀=“translate”;
publicstaticvoidmain(字符串[]args)抛出NoSuchMethodException、InvocationTargetException、IllegalAccessException{
Test003_Translate Translate=新的Test003_Translate();
Map Map=newhashmap();
地图放置(“键001”、“001”);
地图放置(“key002”、“002”);
地图放置(“key003”、“003”);
translate.doTranslate(map,“key001”);
}
私有void doTranslate(映射映射,字符串键)抛出NoSuchMethodException、InvocationTargetException、IllegaAccessException{
字符串actionCode=map.get(key);
方法Method=Test003\u Translate.class.getMethod(前缀+动作码,String.class);
字符串arg=“arg:”;
objects s=method.invoke(this,arg);
}
公共字符串translate001(字符串输入){
返回输入+001;
}
公共字符串translate002(字符串输入){
返回输入+“002”;
}
公共字符串translate003(字符串输入){
返回输入+“003”;
}
}

您可以使用
EnumMap
(比
HashMap
更小更快),如下所示:

public class Test003_Translate {

private static final String PREFIX = "translate";

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Test003_Translate translate = new Test003_Translate();
    Map<String, String> map = new HashMap<>();
    map.put("key001", "001");
    map.put("key002", "002");
    map.put("key003", "003");
    translate.doTranslate(map, "key001");
}

private void doTranslate(Map<String, String> map, String key) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    String actionCode = map.get(key);
    Method method = Test003_Translate.class.getMethod(PREFIX + actionCode, String.class);
    String arg = "arg: ";
    Object s = method.invoke(this, arg);
}

public String translate001(String input){
    return input + "001";
}
public String translate002(String input){
    return input + "002";
}
public String translate003(String input){
    return input + "003";
}
}
enum Key {
    KEY_001,
    ....
}

EnumMap<Key, Runnable> enumMap = new EnumMap<>(Key.class);
enumMap.put(Key.KEY_001, YourClass::translate001);
....

不要使用反射,匿名类,可能使用lambda,应该可以。但是提供更多的细节:你如何确定动作码?我已经知道一个地图和一个键,我可以得到这样的动作码:map.get(key);如果该方法有参数和返回,比如:
公共字符串translate001(字符串输入)
;我不希望translate类有任何字段来存储输入,因为translate方法将被并发调用,我们需要它。@liuliu“输入参数和返回类型”=
java.util.Function
而不是
Runnable
谢谢!拥有一个大约4000行的庞大类是否合适?但我确实需要这些方法。我们正在将数百个字段从旧系统转换到新系统,它们(转换规则)没有任何共同之处!