【问题标题】:Using large IF ELSE string comparison to call different methods. More efficient method?使用大 IF ELSE 字符串比较来调用不同的方法。更有效的方法?
【发布时间】:2012-11-19 10:23:23
【问题描述】:

我使用JQuery 从多个地方调用Java Servlet 以调用servlet 中的多个不同方法。目前,我传递了一个字符串(称为 methodName),然后使用不断增加的 IF ELSE 块来查看要调用哪个函数:

public String methodCaller(String methodName) throws SQLException, IOException
{
    if(methodName.equals("method1"))
    {
        return method1(attr("permit"));
    }
    else if(methodName.equals("method2"))
    {
        return method2(attr("ring"));
    }
    else if(methodName.equals("method3"))
    {
        return method3(attr("gridRef"));
    }
    else if(methodName.equals("method4"))
    {
        return method4(attr("ring"));
    }
    else if(methodName.equals("method6"))
    {
        return method6(attr("ring"), Integer.parseInt(attr("quantity")));
    }

但是,这对我来说似乎非常笨拙且效率低下,尤其是在将来方法的数量会增加的情况下。有没有更有效的方法来比较字符串?或者我应该简单地为每个方法制作一个单独的 servlet(并简单地在processRequest 中进行处理?

【问题讨论】:

  • 你用的是哪个版本的java?
  • java 7 允许切换字符串。这不是更有效,但更令人愉悦。
  • 如果你想花一些时间,这看起来是一个利用反射的好地方。 Map<String, Method> 在这里可以很好地工作。您可以通过创建注释并迭代带注释的方法来填充地图,根据需要将它们放置在地图中。然后您可以使用Method 类中的invoke() 方法来调用您决定需要调用的任何方法。这有点工作,但是一旦你设置好它就会非常干净和可扩展。
  • 我们正在使用 Java 6。谢谢@AlexLynch,我听说过反射,并且可能会研究它,但由于其他因素可能无法在这种情况下使用它影响哪些技术可以使用,哪些技术不能使用。

标签: java ajax string servlets comparison


【解决方案1】:

我会建议,您应该考虑使用 RESTful Web 服务。REST 定义了一组架构原则,您可以根据这些架构原则设计专注于系统资源的 Web 服务,包括如何通过 HTTP 处理和传输资源状态。用不同语言编写的客户。

有很多开源可以帮助您非常轻松地实现restful servlet...其中之一是Apache Wink http://incubator.apache.org/wink/

IBM Developerworks 中有同样的好文章

http://www.ibm.com/developerworks/web/library/wa-apachewink1/

http://www.ibm.com/developerworks/web/library/wa-apachewink2/index.html

http://www.ibm.com/developerworks/web/library/wa-apachewink3/index.html

其他选择:

Spring MVC

Apache CXF http://cxf.apache.org/docs/jax-rs.html

【讨论】:

  • 谢谢,对于这种特殊情况,这可能有点矫枉过正。不过,我肯定会研究它,因为应用程序可能会大幅增长,这可能是最有效的方法。
【解决方案2】:

我建议将您的每个方法都设为实现简单接口的对象。在您的类中,创建一个 HashMap,将接口的每个实现链接到其各自的键。

界面

public interface MyMethod {
   public String call();
}

实施

   public class MethodOne implements MyMethod{

   }

映射和调用

    private Map<String, MyMethod> mappings = new HashMap<String,MyMethod>();

    static{
        mappings.put("method1", new MethodOne());
        //.. other mappings
    }

   public String methodCaller(String methodName) throws SQLException, IOException
   {
      MyMethod myMethod = mappings.get(methodName);
      return myMethod.call();
   }

【讨论】:

  • 我喜欢这个主意,谢谢。我认为通过methodCaller将参数传递给调用方法不会有问题,它会像往常一样工作吗?
  • @Spam 不会有任何问题,但如果它们基本上是字符串文字,我不会通过它们。如果您要传递字符串文字,我会将它们放在类中。
  • @SpAm 作为后续,此设计松散地遵循开放封闭原则。静态映射违反了原则,但正如我所说的“松散地”遵循。
  • 目前对我来说效果很好。如果可能的话,我稍后可能会对其进行调整以更适合最佳实践,但目前,它使我的代码更加整洁和动态,所以谢谢。
【解决方案3】:

我会简单地开始使用整数。

在你的 javascript 中有这样的函数

function convertmethod(methodname)
    {
    thereturn = -1;
    switch(methodname) {
        case "Method1" : thereturn = 1;break;
        case "Method2" : thereturn = 2;break;
        case "Method3" : thereturn = 3;break;
        case "Method4" : thereturn = 4;break;
        }
    return thereturn;
    }

那么在你的java代码中你也可以使用开关

public String methodCaller(int methodName) throws SQLException, IOException
    {
    switch(methodName) 
        {
        case 1: return method1(attr("permit"));break;
        case 2: return method2(attr("permit"));break;
        case 3: return method3(attr("ring"));break;
        case 4: return method4(attr("gridRed"));break;
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 2021-06-01
    • 2016-12-15
    • 2012-05-31
    • 1970-01-01
    相关资源
    最近更新 更多