Lecture thumbnail 0:03 / 3:33 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 you are.

Let’s say you’re working for Volkswagen and you decide that whenever somebody does a mission test on

your cars, your computers are 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, oh, 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 fine.

The CEO could face responsibility for these kinds of actions and so on and so forth.

So this is a typical 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.

Now, let’s take a look at another example of a chain of responsibility.

Let’s suppose you click 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 WPF, for example, is that there is a whole chain

of responsibility in terms of the components which can actually handle the clicking event.

So first of all, if somebody clicks a button, then the button can handle it.

The button can say, Oh, it’s okay, 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 box.

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 window.

So after the button has handled the event and the group box that handled 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, a computer game, so you have

a bunch of creatures that you’re doing battle with, and each creature has attack and defense values.

But interestingly enough, those attack and defense values, they can also be boosted by other cards.

So you can play one card on top of another and boost that card.

So once again, if you want to fight 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 of responsibility.

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 command or query.

And this largely depends on the scenario that you’re dealing with.

Stop Play Play Play Start Play information alert