如何使用构造函数建立下一个链的初始化责任链?

时间:2016-11-09 14:47:29

标签: java chain-of-responsibility

如何使用构造函数构建一个责任链来初始化链。 这是我的Runner.class,我使用 setNext 方法构建我的链。

public class Runner {

private static final String ENTER_PATH = "Enter path: ";
private static final String FILTER_BY_NAME = "Filter by name? (0/1)";
private static final String ENTER_NAME_PARAMETER = "Enter name parameter please: ";
private static final String FILTER_BY_EXTENSION = "Filter by extension? (0/1)";
private static final String ENTER_EXTENSION_PARAMETER = "Enter extension please (without '.'): ";
private static final String FILTER_BY_SIZE = "Filter by size? (0/1)";
private static final String ENTER_SIZE_TO = "Enter upper limit searching files in kb";
private static final String ENTER_SIZE_FROM = "Enter lower limit searching files in kb";
private static final String TRUE="1";
private Scanner scanner;

private List<File> fileList;


public Runner() {
    this.scanner = new Scanner(System.in);
    this.fileList = new ArrayList<>();
}

private String getDirName(){
    System.out.println(ENTER_PATH);
    return scanner.nextLine();
}


private Handler getChainOfResponsibility(){
    GeneralHandler generalHandler = new GeneralHandler();
    Handler lastHandler = generalHandler;


    System.out.println(FILTER_BY_NAME);
    if (scanner.nextLine().equals(TRUE)) {
        System.out.println(ENTER_NAME_PARAMETER);
        String name = scanner.nextLine();
        Handler nameHandler = new NameHandler(name);
        lastHandler.setHandler(nameHandler);
        lastHandler = nameHandler;
    }

    System.out.println(FILTER_BY_EXTENSION);
    if (scanner.nextLine().equals(TRUE)) {
        System.out.println(ENTER_EXTENSION_PARAMETER);
        Handler extensionHandler = new ExtensionHandler(scanner.nextLine());
        lastHandler.setHandler(extensionHandler);
        lastHandler = extensionHandler;
    }


    System.out.println(FILTER_BY_SIZE);
    if (scanner.nextLine().equals(TRUE)) {
        System.out.println(ENTER_SIZE_FROM);
        double fromSize = scanner.nextDouble();
        System.out.println(ENTER_SIZE_TO);
        double toSize = scanner.nextDouble();
        Handler sizeHandler = new SizeHandler(fromSize, toSize);
        lastHandler.setHandler(sizeHandler);
    }
    return generalHandler;
}


public void run() throws InterruptedException, IOException {
    findAllFiles(getDirName(), getChainOfResponsibility());
    showListOfFilteredFiles();

}

这是我的Handler抽象类

    public abstract class Handler {

    private Handler handler;

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    protected abstract boolean handleFilter(File file);

    public boolean doFilter(File file){
        if(handleFilter(file)){
            if(Objects.nonNull(handler)){
                return handler.doFilter(file);
            }
            return true;
        }
        return false;
    }

}

我也有它的实施。如何制作,它将通过构造函数初始化!请帮忙!

1 个答案:

答案 0 :(得分:1)

序言

你的实施是错误的;实现Chain of Responsibility模式的一般方法是链中的每个链接如果请求本身无法处理请求,则将请求转发到下一个链接。传统上,如果当前类无法处理请求,则委托给抽象super类。简而言之,为了遵守传统,我会按照以下方式重写您的Handler课程:

public abstract class Handler {
  private Handler next;

  public void setHandler(Handler next) {
    this.next = next;
  }

  public boolean doFilter(File file) {
    return next.doFilter(file);
  }
}

我还没有阅读您的HandlerNameHandler课程的合同,但我只是做了一些假设,以便提供NameHandler的示例。重构其父级后可能看起来像:

public class NameHandler extends Handler {
  private final String name;

  public NameHandler(String name){
    this.name = name;
  }

  @Override
  public boolean doFilter(File file){
    final boolean result;

    if(file.getName().equals(name)){
      result = true;
    }else{
      result = super.doFilter(file);
    }

    return result;
  }
}

答案

使用构造函数来配置链中的下一个链接,而不是&#34; setters&#34;就像你的setHandler方法一样:

public abstract class Handler {
  private final Handler next;

  public Handler(Handler next){
    this.next = next;
  }

  public boolean doFilter(File file){
    return next.doFilter(file);
  }
}

现在您的子类需要处理非默认构造函数:

public class NameHandler extends Handler {
  private final String name;

  public NameHandler(String name, Handler next){
    super(next);
    this.name = name;
  }

  @Override
  public boolean doFilter(File file){
    final boolean result;

    if(file.getName().equals(name)){
      result = true;
    }else{
      result = super.doFilter(file);
    }

    return result;
  }
}