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
}
}
}
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 dostatement.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.
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