小编SBm*_*ore的帖子

当唯一部分位于循环/ try-catch中时,避免重复代码

我有一个类有两个方法,有很多重复的代码但是唯一的位在整个事件的中间.根据我的研究,我认为我应该做"执行周围方法"模式,但我找不到我可以遵循的资源,因为他们似乎都使用我无法复制的代码.

我有两种方法,apiPost和apiGet,我已粘贴在下面.我已经将这些方法的独特部分包装在一起,注释显示了唯一部分的开始和结束位置:

/**
 * Class that handles authorising the connection and handles posting and getting data
 *
 * @version     %I%, %G%
 * @since       1.0
 */
public class CallHandler {
    private static PropertyLoader props = PropertyLoader.getInstance();
    final static int MAX = props.getPropertyAsInteger(props.MAX_REQUESTS);
    private final Logger log = LoggerFactory.getLogger(CallHandler.class);
    private final static String POST = "POST";
    private final static String GET = "GET";

    /**
     * Makes a POST call to the API URL provided and returns the JSON response as a string …
Run Code Online (Sandbox Code Playgroud)

java code-duplication

4
推荐指数
1
解决办法
91
查看次数

标签 统计

code-duplication ×1

java ×1