我该如何分解这段代码?

时间:2014-04-29 14:21:43

标签: java function call

我正在研究一个调用Web服务的java webserver。我试图更好地编写一些代码。

我有两个类似的功能,只有一件事发生了变化:我必须根据自己的意愿打电话。

public Object updateDef() {
    ExecutorService pool = Executors.newFixedThreadPool(20);
    List<Callable<String>> tasks = new ArrayList<Callable<String>>();

    logger.info("Updating surv def...");

    for (final Surv surv : Client.activatedSurvs) {
        final Client client = new Client(surv.getName(), surv.getPassword());

        tasks.add(new Callable<String>() {
            public String call() throws Exception {
                HERE -> client.updateDef(surv);
                return surv.getId();
            }
        });
        client.destroy();
    }

    List<Future<String>> results = null;
    try {
        results = pool.invokeAll(tasks);
        for (Future<String> future : results) {
            future.get();

        }
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    pool.shutdown();

    return null;
}

另一个:

public Object updateStats() {
    ExecutorService pool = Executors.newFixedThreadPool(20);
    List<Callable<String>> tasks = new ArrayList<Callable<String>>();

    logger.info("Updating surv Stats...");

    for (final Surv surv : Client.activatedSurvs) {
        final Client client = new Client(surv.getName(), surv.getPassword());

        tasks.add(new Callable<String>() {
            public String call() throws Exception {
                HERE -> client.updateStats(surv).toString();
                return surv.getId();
            }
        });
        client.destroy();
    }

    List<Future<String>> results = null;
    try {
        results = pool.invokeAll(tasks);
        for (Future<String> future : results) {
            future.get();
        }
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    pool.shutdown();

    return null;
}

因此,2个函数是来自A类的updateStats和updateDef,带有来自B的静态列表。 问题是我要传递一个参数,该参数是此列表中的现有元素。

=&GT; A.updateStats(B.list.get(X));

=&GT; A.updateDef(B.list.get(X));

我访问了此链接How to pass a function as a parameter in Java?但未成功

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您可以使用策略模式或Functors。老实说,我认为我只会将常见的部分分解为共享方法。

答案 1 :(得分:0)

轻松 - 传递Callable并且不要重复它周围的所有样板。

public Object foo(Callable<String> callable) {
    ExecutorService pool = Executors.newFixedThreadPool(20);
    List<Callable<String>> tasks = new ArrayList<Callable<String>>();

    logger.info("Updating surv def...");

    for (final Surv surv : Client.activatedSurvs) {
        final Client client = new Client(surv.getName(), surv.getPassword());
        tasks.add(callable;
        client.destroy();
    }

    List<Future<String>> results = null;
    try {
        results = pool.invokeAll(tasks);
        for (Future<String> future : results) {
            future.get();

        }
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    pool.shutdown();

    return null;
}

答案 2 :(得分:0)

您的目标是替换:

tasks.add(new Callable<String>() {
        public String call() throws Exception {
            client.updateStats(surv).toString();
            return surv.getId();
        }
    });

使用

tasks.add(new Callable<String>() {
        public String call() throws Exception {
            doSomething(client, surv);
            return surv.getId();
        }
    });

可以通过定义接口来完成:

public interface ClientUpdate {
    public void performUpdate(Client client, Surv surv);
}

然后,使用此接口作为方法的参数:

public Object updateStats() {

变为

public Object update(ClientUpdate updater) {

然后在你的可调用

中使用它
tasks.add(new Callable<String>() {
        public String call() throws Exception {
            updater.performUpdate(client, surv);
            return surv.getId();
        }
    });

然后实施它:

updateStats();

变为:

update(new ClientUpdate() {
    public void performUpdate(Client client, Surv surv) {
        client.updateStats(surv).toString();
    }
});

同样适用于updateDef();