Select Page

Chain of responsability

Chain of responsability

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Source code

/**  * Handles request it is responsible to.  */ public class ConcreteHandler extends Handler {         /**   * Handle request or delegate it.   */         public void handleRequest() {                 boolean canProcessThisRequest = false;                if (canProcessThisRequest) {                         // handle request if possible                 } else {                         // pass it to the next in chain                         super.handleRequest();                }         }  }  /**  * Defines interface for request handling  * @role __Handler  */ public class Handler {         private Handler successor;        /** Default request handling */         public void handleRequest() {                 if (successor != null) {                         successor.handleRequest();                }         }          public Handler getSuccessor() {                 return this.successor;        }          public void setSuccessor(Handler successor) {                 this.successor = successor;        }  }     

About The Author

Cédric Walter

I worked with various Insurances companies across Switzerland on online applications handling billion premium volumes. I love to continuously spark my creativity in many different and challenging open-source projects fueled by my great passion for innovation and blockchain technology.In my technical role as a senior software engineer and Blockchain consultant, I help to define and implement innovative solutions in the scope of both blockchain and traditional products, solutions, and services. I can support the full spectrum of software development activities, starting from analyzing ideas and business cases and up to the production deployment of the solutions.I'm the Founder and CEO of Disruptr GmbH.