单一责任原则的重构方法

时间:2013-02-27 13:05:58

标签: java refactoring single-responsibility-principle

我需要测试合同义务计划 我没有看到任何直接的方法来测试这个方法。这违反了Single Responsibility Principle,只是做了太多事情 我知道,如何安全地将以下责任提取到新的方法或类中:

public void askUserPathAndWord() {

        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(System.in));
        String path;
        String whatFind;
        BlockingQueue<File> queue = new LinkedBlockingQueue<File>();

        try {
            System.out.println("Please, enter a Path and Word"
                    + "(which you want to find):");
            System.out.println("Please enter a Path:");
            path = bufferedReader.readLine();
            System.out.println("Please enter a Word:");
            whatFind = bufferedReader.readLine();

            if (path != null && whatFind != null) {

                File endOfWorkFile = new File("GameOver.tmp");
                CountDownLatch latch = new CountDownLatch(2);

                FolderScan folderScan = new FolderScan(path, queue, latch,
                        endOfWorkFile);
                FileScan fileScan = new FileScan(whatFind, queue, latch,
                        endOfWorkFile);

                Executor executor = Executors.newCachedThreadPool();
                executor.execute(folderScan);
                executor.execute(fileScan);

                latch.await();
                System.out.println("Thank you!");
            } else {
                System.out.println("You did not enter anything");
            }

        } catch (IOException | RuntimeException e) {
            System.out.println("Wrong input!");
            e.printStackTrace();
        } catch (InterruptedException e) {
            System.out.println("Interrupted.");
            e.printStackTrace();
        }
    }

Qustions:
我们如何能够安全地完成下一步:

  • 询问用户的文字和路径;
  • 一句话,创建一个FileScan 用正确的参数(工厂);
  • 给定一条路径,创建一个 FolderScan具有正确的参数(工厂);
  • 鉴于两个 runnables和一个latch,创建一个ExecutorService,对它们进行排队,然后等待 在闩锁上。

2 个答案:

答案 0 :(得分:3)

与大多数重构一样,安全的第一步是测试目前的方法。 然后将代码重构为具有必要职责的逻辑组 - 在整个过程中,您之前编写的测试不应该破坏。 然后你可以放弃原来的测试,测试太多的东西,而不是仅仅对你编写的新代码进行断言的测试

答案 1 :(得分:1)

这就是我重构该方法的方法,主要使用IDE的提取方法功能和常识。最好为该方法编写一个测试,以确保重构没有破坏任何东西。

class PathAndWord {
    final String path;
    final String whatFind;

    PathAndWord(String path, String whatFind) {
        this.path = path;
        this.whatFind = whatFind;
    }

    boolean isProperlyInitialized() {
        return path != null && whatFind != null;
    }
}

public void askUserPathAndWord() {
    try {
        tryToAskUserPathAndWord();
    } catch (IOException | RuntimeException e) {
        System.out.println("Wrong input!");
        e.printStackTrace();
    } catch (InterruptedException e) {
        System.out.println("Interrupted.");
        e.printStackTrace();
    }
}

private void tryToAskUserPathAndWord() throws IOException, InterruptedException {
    PathAndWord pathAndWord = readPathAndWord();

    if (pathAndWord.isProperlyInitialized()) {
        performScan(pathAndWord, "GameOver.tmp");
        System.out.println("Thank you!");
    } else {
        System.out.println("You did not enter anything");
    }
}

private PathAndWord readPathAndWord() throws IOException {
    System.out.println("Please, enter a Path and Word (which you want to find):");

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

    String path = readPath(bufferedReader);
    String whatFind = readWord(bufferedReader);
    return new PathAndWord(path, whatFind);
}

private String readPath(BufferedReader bufferedReader) throws IOException {
    System.out.println("Please enter a Path:");
    return bufferedReader.readLine();
}

private String readWord(BufferedReader bufferedReader) throws IOException {
    System.out.println("Please enter a Word:");
    return bufferedReader.readLine();
}

private void performScan(PathAndWord pathAndWord, String endOfWorkFileName) throws InterruptedException {
    BlockingQueue<File> queue = new LinkedBlockingQueue<File>();

    File endOfWorkFile = new File(endOfWorkFileName);
    CountDownLatch latch = new CountDownLatch(2);

    FolderScan folderScan = new FolderScan(pathAndWord.path, queue, latch,
            endOfWorkFile);
    FileScan fileScan = new FileScan(pathAndWord.whatFind, queue, latch,
            endOfWorkFile);

    Executor executor = Executors.newCachedThreadPool();
    executor.execute(folderScan);
    executor.execute(fileScan);

    latch.await();
}
相关问题