/**
 * Chain of Responsibility is a behavioral design pattern that passes
 * requests along a chain of handlers. Each handler decides either to
 * process the request or to pass it to the next handler in the chain.
 */ interface Handler { setNext ( handler ) : Handler ; handle ( request ) : string | void ; } abstract class AbstractHandler implements Handler { private nextHandler ? : Handler ; handle ( request ) { if ( this . nextHandler ) { return this . nextHandler . handle ( request ) ; } return undefined ; } public setNext ( handler ) { this . nextHandler = handler ; return handler ; } } export class MonkeyHandler extends AbstractHandler { public handle ( request ) { if ( request === 'Banana' ) { return `Monkey: I'll eat the ${ request }.` ; } return super . handle ( request ) ; } } export class SquirrelHandler extends AbstractHandler { public handle ( request ) { if ( request === 'Nut' ) { return `Squirrel: I'll eat the ${ request }.` ; } return super . handle ( request ) ; } } export class DogHandler extends AbstractHandler { public handle ( request ) { if ( request === 'MeatBall' ) { return `Dog: I'll eat the ${ request }.` ; } return super . handle ( request ) ; } }