Why would I use a chain of responsibility over a s

2019-06-16 05:03发布

Consider you got several validations. Those validations should only take effect if the object to be inspected is of a certain type. Why would I use a chain of responsibility over a switch-statement?

Example with chain of responsibility

public class Executor {

@Inject
private ValidatorFactory validatorFactory;

public void execute(Konfiguration konfig) {
    List<Statement> statements = konfig.getStatements();
    AbstractValidator validator = validatorFactory.create();
    for (Statement statement : statements) {
        if (validator.validate(statement.getType())) {
            crudService.execute(statement.getSql());
        }
    }
}

The validatorFactory creates the chain of Validators. One validator would look like

public class AddPrimaryKeyValidator extends AbstractValidator {

@Override
public boolean validate(Statement statement) {
    if (SqlType.ADD_PK.getTyp().equals(statement.getType())) {
        return doesTableAndPrimaryKeyExist(statement.getTabName());
    }
    return successor.validate(statement);
}

Example with switch-statement

public void execute(Konfiguration konfig) {
    List<Statement> statements = konfig.getStatements();
    for (Statement statement : statements) {
        switch (statement.getType()) {
        case "ADD_PK":
            if (doesTableAndPrimaryKeyExist(statement.getTabName())) {
                frepCrudService.execute(statement.getSql());
            }
            // more cases
        }
    }
}

2条回答
爷的心禁止访问
2楼-- · 2019-06-16 05:46

Looks like an odd reason to use a chain of responsability. You are basically building a chain only to create a dynamic list of if statements, which is probably not dynamic anyway since I'm sure you hard-coded the chain initialization with one validator per statement.

I believe that chain of responsability is not the right pattern for this. You should replace if statements by polymorphism instead.

For instance if you have specialized Statement classes you can just do statement.validate(). Statement doesn't have to validate itself if you don't want to, it can just know which validator to use internally and delegate the validation to it.

If you do not have specialized statement classes you could ask the factory for the proper validator right away instead of building the entire chain.

查看更多
够拽才男人
3楼-- · 2019-06-16 06:03

Because in a chain of responsibility you don't need to know who does what up front in the caller. The logic of decide when you are about to run your piece of code in a chain is owned by you and the rest of the code can ignore it. This allow to encapsulate specific logic in the right place. Servlet filters are a good example of this

查看更多
登录 后发表回答