简体   繁体   English

避免Java方法中的代码重复

[英]avoiding code duplication in a java method

Apologies if this has already been asked/answered a thousand times (I did check first). 抱歉,如果已经问过一千遍了(我先检查了一下)。

I'm not a Java programmer by trade and have been tasked with extending an existing Java SOAP service. 从本质上讲,我不是Java程序员,而是受命扩展现有的Java SOAP服务。 I'm keen to avoid copy/pasting existing code that I know works, but was wondering what the best options available in java are. 我渴望避免复制/粘贴我知道有效的现有代码,但想知道java中可用的最佳选项是什么。

Essentially I have this method already: 本质上我已经有了这种方法:

public String methodThatIDontWantToCopyPaste(
        @WebParam(name = "a", partName = "a") String paramA,
        @WebParam(name = "b", partName = "b") int paramB,
        @WebParam(name = "c", partName = "c") int paramC) {

    // Validate parameters
    if (paramA.isEmpty() || 
            0 == paramB || 
            0 == paramC) {
        return "Invalid request";
    }
    String response = "";

    try {

        // Parmaeters OK, build client
        /*
        lots of generic implementation here
        ...
        XYZ client = ...
        ...
        */

        response = client.specificMethodToHandleABC(paramA, paramB, paramC);

    } catch (SOAPException | IOException ex) {
       // handling omitted

    } 

    return response;
}

I want to add several new/similar methods, but each will have: 我想添加几种新的/类似的方法,但是每种方法都有:

  1. A different set of parameters 一组不同的参数
  2. A different block of code to validate the parameters (the above code is trivial, but some will be more detailed 用于验证参数的不同代码块(上面的代码很简单,但是有些会更详细)
  3. A different implementation of the line: response = client.specificMethodToHandleABC(a, b, c); 该行的另一种实现:response = client.specificMethodToHandleABC(a,b,c); ie where a different method will be called, with a different set of arguments 也就是说,将调用具有不同参数集的其他方法

I'd normally go for a callback in my usual programming language, but it's a proprietary language and not as powerful as Java, so I wanted to know what the best option was? 我通常会使用常用的编程语言进行回调,但是它是一种专有语言,没有Java强大,所以我想知道最好的选择是什么?

In a similar setting, I used callbacks/anonymous classes by calling a method out of every endpoint and passing a callback for every variable part into the method, like the following 在类似的设置中,我通过在每个端点之外调用一个方法并将每个变量部分的回调传递给该方法来使用回调/匿名类,如下所示

public String methodA(final int param1, final String param2) throws Exception {
    return this.call(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return param1 != 0 && param2 != null;
        }
    });
}


public String methodB(final String param1, final String param2) throws Exception {
    return this.call(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return param1 != null && param2 != null;
        }
    });
}

private String call(Callable<Boolean> validationCallable) throws Exception {
    // static code similar to all methods

    assert validationCallable.call().equals(Boolean.TRUE);

    // static code similar to all methods

    return ""; // probably result based on another callable
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM