Purpose: Behavioural Type: Object
Used to achieve loose coupling where a request from a client is passed to a chain of objects to process them. The object in the chain will decide whether the request moves onto the next object in the chain or not.
Applicable when:
Pros:
Cons:
class Handler { private _successor: Handler; public set successor(successor: Handler) { this._successor = successor; } public get successor() { return this._successor; } public handleRequest(msg: Number): void {} } class ConcreteHandlerA extends Handler { handleRequest(req: Number): void { if (req > 0) { console.log(`Handler A process ${req}`); } else { this.successor.handleRequest(req); } } } class ConcreteHandlerB extends Handler { handleRequest(req: Number): void { if (req < 0) { console.log(`Handler B process ${req}`); } else { this.successor.handleRequest(req); } } } class ConcreteHandlerC extends Handler { handleRequest(req: Number): void { if (req === 0) { console.log(`Handler C process ${req}`); } else { this.successor.handleRequest(req); } } } (function main() { const reqA = new ConcreteHandlerA(); const reqB = new ConcreteHandlerB(); const reqC = new ConcreteHandlerC(); // set the chain or responsibility reqA.successor = reqB; reqB.successor = reqC; // handle the requests reqA.handleRequest(0); // logs "Handler C process 0" reqA.handleRequest(1); // logs "Handler A process 1" reqA.handleRequest(-1); // logs "Handler B process -1" })();