Pattern · Core

Chain of Responsibility

Pass a request along a chain of handlers; each either handles it or forwards it to the next — so the sender doesn't need to know who will deal with it.

Asked atAmazonGoogleAtlassian
step 1 / 8
1/2The chain
next
«abstract»
Approver
- next: Approver
+ handle(req)
+ setNext(a)
Manager
+ handle()
Director
+ handle()
CEO
+ handle()
The chain
1abstract class Approver { Approver next; void setNext(Approver a){...} void handle(Request r){...} }
2class Manager extends Approver { ... } // limit $1000
3class Director extends Approver { ... } // limit $10000
4class CEO extends Approver { ... } // unlimited
State
holdsnext: Approver

The abstract Approver holds a `next` handler — the self-pointing diamond IS the chain link — and defines handle() to either approve or forward, plus setNext() to wire the chain.