2017-10-29 63 views
-1

我试图在Java中实现一个动作/反应系统。
为此,我需要将所有方法存放在容器中,以便我可以轻松地调用所需的响应,并返回所需的操作。
作为一名C++开发人员和Java新手,我的第一个直觉就是创建一个函数指针数组(至少重现它),所以我尝试使用匿名子类。但没有得到我期待的结果。
所以我尝试使用lambdas,这里是我想要做的一个示例。Java方法容器

public class Test { 
     public Map<Integer, Vector<String>> actions = new HashMap<>(); 
     public Map<Integer, Integer> responses = new HashMap<>(); 

     public Test() { 
     Vector<String> v= new Vector<String>(); 

     actions.put(0, action0()); 
     actions.put(1, action1()); 
     responses.put(0, response0(Vector<String>)); // How can I leave aside this argument which I don't know at this point ? 
     responses.put(1, response1(Vector<String>)); 
     } 

     public Vector<String> action0() {...} 
     public Vector<String> action1() {...} 

     // This methods takes actions return as argument 
     public Integer response0 (Vector<String>) {...} 
     public Integer response1 (Vector<String>) {...} 

     public void run() { 
     // When I run, I want to be able to launch any of my responses with any of my actions return 
     responses.get(0) 
     } 
    } 

我至少试过一个很好的方法来解决这个问题吗?
非常感谢

+0

我不明白你的问题很好。你想要叫做“行动”,并与该返回值调用“回应”? –

+0

我不想偏离主题,但除非这种方法是强制性的,否则您可能需要查看'RxJava'。 – ChiefTwoPencils

+0

...因为你从C++到Java,你确定[[Vector']](https://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete - 或不推荐使用)是你想使用的? – ChiefTwoPencils

回答

-2

代码有一些缺陷,所以我会假设 getHashtag()action1()都应该是一样的,和类似的其他三个。

另外,我假设第二 responses.put()应该是关键 1,不 0另外,您的responseX()方法的参数需要一个名称。

无论如何,你需要一个功能接口,所以你可以给responseX()方法Method References

对于您的情况,responseX()方法采用Vector<String>作为参数,并返回Integer,因此功能接口将为Function<Vector<String>, Integer>

然后,您可以构建这些方法的映射,稍后执行。

public Test() { 
    // Here we can build map of response methods first, if we like, even though Vectors don't exist yet 
    Map<Integer, Function<Vector<String>, Integer>> responseMethods = new HashMap<>(); 
    responseMethods.put(0, this::response0); 
    responseMethods.put(1, this::response1); 

    // Now we build the action map of Vectors 
    Map<Integer, Vector<String>> actions = new HashMap<>(); 
    actions.put(0, action0()); 
    actions.put(1, action1()); 

    // At this time, we can now execute the referenced methods to get the actual responses 
    Map<Integer, Integer> responses = new HashMap<>(); 
    for (Integer key : actions.keySet()) { 
     Vector<String> v = actions.get(key); 
     Function<Vector<String>, Integer> responseMethod = responseMethods.get(key); 
     Integer response = responseMethod.apply(v); 
     responses.put(key, response); 
    } 
} 

public Vector<String> action0() {...} 
public Vector<String> action1() {...} 

public Integer response0(Vector<String> v) {...} 
public Integer response1(Vector<String> v) {...} 

,如果你愿意,你甚至可以推迟的行动方法的执行:

public Test() { 
    // Here we can build map of response methods first, if we like, even though Vectors don't exist yet 
    Map<Integer, Function<Vector<String>, Integer>> responseMethods = new HashMap<>(); 
    responseMethods.put(0, this::response0); 
    responseMethods.put(1, this::response1); 

    // Now we build the action map of Vectors 
    Map<Integer, Supplier<Vector<String>>> actionMethods = new HashMap<>(); 
    actionMethods.put(0, this::action0); 
    actionMethods.put(1, this::action1); 

    // At this time, we can now execute the referenced methods to get the actual responses 
    Map<Integer, Integer> responses = new HashMap<>(); 
    for (Integer key : actionMethods.keySet()) { 
     Supplier<Vector<String>> actionMethod = actionMethods.get(key); 
     Function<Vector<String>, Integer> responseMethod = responseMethods.get(key); 
     Vector<String> v = actionMethod.get(); 
     Integer response = responseMethod.apply(v); 
     responses.put(key, response); 
    } 
}