samedi 18 mars 2023

Chain of responsibility special scenario

In normal chain of responsibility design pattern implementation, handlers of the request will be added to the chain in a sequential order and will be executed accordingly. However in a special scenario if it's required to skip intermediate handlers and process request in the final handler, will it violate COR design pattern? Please refer the code snippets below. Hope it will clarify this further. Thanks in advance.

public class SomeHandler extends RequestProcessor {

public SomeHandler() {
    super();
}

@Override
public void process(Request request) {
    if (request != null) {
        //Logic to decide request type
        if ("TYPE_ONE".equalsIgnoreCase(request.getType())) {
            //passing to next object in the chain. Multiple chains...
            this.nextRequestProcessor.process(request);
        } else if ("TYPE_TWO".equalsIgnoreCase(request.getType())) {
            //passing to the last object in the chain,skip all the other intermediate objects
            new FinalHandler().process(request);
        }
    }
}

}

Aucun commentaire:

Enregistrer un commentaire