In this section of the course we're going to take a look at the Chain of Responsibility design pattern.
So what is it all about.
Well let's imagine a situation where your let's say you're working for Volkswagen and you decide that
whenever somebody does emission tests on your cars your computer is not going to lie.
They're going to reduce the toxic output so that everybody thinks your cars are the most environmentally
friendly.
So this is clearly unethical behavior.
And the question is well who actually takes the blame for this behavior.
And it typically goes up the chain.
So first of all you can just blame an employee.
We can say all this guy he messed up.
He tried to cheat and we're going to fire him and just forget it ever happened.
However this guy typically has a manager and we have to ask the question well did the manager know about
this because if the manager knew about this well it could be you know the department policy to just
lie and cheat and whatever.
Or maybe the CEO knew about this in which case the entire company is corrupt and the company should
be fined the CEO could face responsibility for these kinds of actions and so on and so forth.
So this is atypical illustration of the chain of responsibility.
So first of all we have the employee then we have the manager then we have the CEO and they are all
responsible.
And depending on the kind of unethical behavior that has occurred the actual consequences may impact
one or more of these individuals in the chain.
Let's take a look at another example of a chain of responsibility.
Let's suppose you create some graphical element on a form.
So for example you click a button and the question is well who actually handles this event.
And typically the way this is done in systems like WPA for example is that there is a whole chain of
responsibility in terms of the components we can actually handle the clicking event.
So first of all some of the clicks a button then the button can handle it.
The button can say oh it's ok.
It's my responsibility I'm going to handle it and I'm going to stop any further processing so that the
event doesn't go up the chain.
But then the button is actually part of a group block.
So the group box can then receive the event and handle it in its own special way.
And then you have let's say you have the underlying windows so after all the button has handled the
event and the group box would handle the event.
You can also have the window itself handle the event.
And this is once again a chain of responsibility.
Now another example is let's say you have a collectible card game computer game games so you have a
bunch of creatures that you're doing battle with.
And each creature has attack and defense value.
But interestingly enough those attack in defense Fatty's they can also be boosted by other cards so
you can play one card and top of another and boost that card.
So once again if you want to find the new and improved attack and defense values for that card you have
to walk the chain of responsibility by looking not just at the underlying card but all the other cards
which boosted this particular card.
And this is once again an illustration of the chain irresponsibilities so the chain of responsibility
is essentially just a chain of components who all get a chance to process either a command or a query
and they can optionally have some sort of default processing implementation and they can also terminate
the processing chain and thereby preventing the rest of the objects in the chain to actually prevent
them from processing this particular commander query and this largely depends on the scenario that you're
dealing with.