December 2018
Intermediate to advanced
500 pages
12h 19m
English
Many times, the commands described previously might need handling such that we want a receiver to do work only when it is able to, and it's not then hand off the command to someone else next in the chain. For example, for the reports use case, we might want to handle authenticated and unauthenticated users differently.
The Chain of Responsibility pattern enables this by chaining a set of receiver objects. The receiver at the head of the chain tries to handle the command first, and if it's not able to handle it, delegates it to the next.
The sample code is given here:
type ChainedReceiver struct { canHandle string next *ChainedReceiver}func (r *ChainedReceiver) SetNext(next *ChainedReceiver) { r.next = next}func (r ...Read now
Unlock full access