The Chain of Responsibility design pattern provides a solution:
Define a chain of Handler
objects
having the responsibility
to either handle a request
or forward it to the next handler.
A class sends a request
to a chain of handlers
and doesn't know (is independent of) which handler
handles the request.
Describing Chain of Responsibility in more
detail is the theme of the following sections.
See Applicability section for all problems Chain of Responsibility can solve.
Handler
objects:
Handler | handleRequest()).
Handler interface
by either handling the request directly or
forwarding it to the next handler (if any) on the
chain.
Sender) sends a request to a chain of handlers
(handler.handleRequest()).
The request gets passed along the chain until a handler
handles it.
Handler objects can be specified
dynamically at run-time
(Handler objects can
be added to and removed from the chain).