All right.
So we're going to begin our discussion of the chain of responsibility design pattern by looking at the
kind of example that you find in the original Gang of Four book which means it's not particularly modern.
So what we're going to be building is we're going to be building essentially a pointer chain and you
can think of it as a singly linked list full of pointers so nothing particularly exciting but at least
you're going to see the scenarios that this is relevant for so let's imagine that you have a computer
game and that computer game has creatures roaming the ground so I'm going to define a creature so a
creature might have a name.
It might also have certain attack and defense values and I'm going to make a constructor which actually
initializes all of this and maybe a stream out of the operator as well so we can print the thing stream
put the operator though.
OK so this is my creature and now what I want to do is I want to simulate situations where the creature
gets some of its abilities modified so perhaps the creature picks up a magic sword or maybe some some
enchantment is cast on the creature and the creature has a boost to its abilities which we want to calculate.
So the question is well how can we calculate it now in the classic implementation of the chain of responsibility
what you would do is you would define a creature modifier.
So we're going to have a class called Creature modifier.
So this is a class which is going to take the creature and it is going to modify some of its arguments
and the point about a chain of responsibility is that you can have several modifiers and a creature.
So for example if a creature both picks up a magic sword and gets enchanted then both of these effects
actually affect the creature in the proper order in the proper way.
So let's build a creature modify So the idea is that a creature modifier would be a singly linked list
so it's a it's a class which has a pointer to the next member of the same class so we're going to have
a creature modify a pointer called next initially and PVR.
So this allows us to change the creature modifiers together and as you may have guessed the whole point
of this class is inheritance.
The whole point is that you inherit the creature modify and you define what kind of modifications you
actually want to be done on the creature.
We're also going to have a reference to the creature itself so let's make that protected will have a
reference to the creature that we're going to modify and you're welcome to use a pointer or shared BCR
here I'm just using a reference for convenience and then we're going to have a couple of public members.
So first of all we'll have the constructor which actually lets us specify who the creature is modifying
or who the creature modifier is actually affecting.
And here we just take the reference and we pass it.
So we assign it to our field.
We're are also going to have a function called AB which allows us to enlist a new modifier into the
chain so we're going to take a pointer to a creature modifier and we're going to add it to the chain.
Now this is done using effectively the understanding that the next pointer is a pointer in a very long
list.
So what we're going to do is we're going to say if there is a next point here then we're going to take
the next pointer and call and on.
That's because we want to add it to the end of the list and this is how you add an element to the end
of a singly linked list because you don't implicitly know where the list.
And so you just follow the pointer chain until you reach the very end.
Otherwise of course you said next to the current creature modifier.
And this is how you effectively build the list of responsibilities in fact.
So the next part is how do you apply these modifiers to the creature and for that we'll have full of
we'll have a virtual void function called handle.
So this is where you actually perform the modifications and the way this is done is as you can see the
function is virtual.
And what this means is that you are meant to override this function and perform the actual modification
of the creature.
However there is a default behavior that we want to preserve and the default behavior is to follow the
chain so if there is another element in the chain that we take that element and we call handle on that
as well.
Now why is this relevant.
It's relevant because when we inherit the creature modify it we're going to be wanting to in addition
to doing our thing we're going to be wanting to call the base handle invocation.
This particular invocation in order to actually follow the entire chain.
So finally it's time for us to actually make some modifiers So first of all make a double attack by
the fire so I'm going to make them all the fire which actually takes the creature and doubles its attack
value.
So you're going to have a class called Double attack modifier which is going to inherit from Creature
modifier.
And here of course we're going to make an override so let's override the handle function at the very
least.
But in addition what we want is we want the constructor as well so I think we're just going to get it
from here.
We're going to get a constructor which matches the base class constructor and nothing particularly magical
here.
So in the hand that we want to do two things so oh yes we do want to call the base eventually but before
we do that we want to double the tax so we say creature don't attack multiply it equals 2 so we doubled
the attack value of the creature and the second part.
This part is important.
It's really important that you do this because this is precisely what allows us to walk the chain of
responsibility because remember when you call this handle here well you're essentially calling is you're
calling this and this checks the next pointer and goes to the next part of the chain and handles that
as well.
So this is how you can string different mortifies together.
So let's actually add another modifier.
So let's have an increased increase defense modifier
no.
So once again very similar things.
We generate a constructor and we generate the override.
Not this one though.
And we don't need to override what we need to override hand.
And in the hand.
All right well we're going to do is we're going to check whether the hack is less than or equal to 2
if it is we're going to increase defense by 1 so you can have these wonderful conditions so if creature
on that attack is less than or equal to two then what I'm going to do is I'm going to say creature the
fence plus plus for example we're going to increase it by 1.
OK.
So we have all of these modifiers and we can now start using them with this chain of responsibility
so I can make a creature.
So I'm going to call this a goblin.
So a creature called goblin which has an attack of one in defense of one.
And then what we can do is we can make the root modifiers so creature modify and root.
So the road is kind of the start of our singly linked list.
It references a goblin but it doesn't do anything.
So the baseline creature modified doesn't do anything it's just the start of the list but we can add
other modifiers to this particular modifier.
So for example if I want a double attack modifier
let's call it our one.
I can do it like so and I can make another one so I can double the attack again and I can also try to
increase the defense defense modifier too.
OK so we have all of these objects and we can now add them to the roots so I can say roots dog.
And here I can just provide a pointer so I can provide a pointer to our one and duplicate this.
And the second doubling of the attack and then duplicate this again and add to that.
Of course only this one and this one are actually going to affect the creature because remember by the
time you invoke this modifier you are checking whether attack is less than or equal to 2 and it should
be well to 4.
At roughly this point so this should effectively be an no op for us.
So let's actually check that this is in fact the case where we're going to do is we're going to call
RUDOV hand.
And this actually invokes the entire chain of responsibility by walking on the next pointer.
So when you handle it checks the next pointer tries next door Tandou and remember Hondo is virtual.
So as you inherit that it will attack of the fire and increase the offense by fire are you going to
be calling the actual implementation.
You're going to be calling this function you're going to be calling this function.
But it always comes back to actually calling the base class and that's what you call the next handler.
And so on and so forth up until the point where next is actually no PDR in which case you stop and you've
applied all your modifiers.
So now that we've applied everything let's take a look at what the Goldman actually is.
OK.
So let's see if we can run this all right as you can see we have a goblin with attack finally afore
and defense is still one because remember the defense modifier is no all due to the condition that we've
found it.
So this is how you can implement the chain of responsibility.
Now there's one thing that I wanted to kind of mentioned in passing and that is the ability to actually
disable the whole chain.
So let's imagine that at some point in time the call button actually comes across a switch and that
which cast the spell where the goblin can receive no modifiers anyway.
So how can you actually cancel the entire chain of responsibility.
How can you make sure that none of these additional modifiers actually have any effect.
And it's actually very easy because what you do is you simply fail to propagate the base class.
Cole So let me show you how this can be done so we're going to make a kind of no bonuses modifier.
So I'm going to have a class called No bonuses modifier which is going to be a public creature modifier.
So once again we generate all the necessary things so we generate the no we generate the constructor
and then we generate the override to actually implement functions.
Oh wait wait wait.
Override.
So we're going to override the handle.
And here I'm going to delete everything.
I'm going to delete the base class.
So what does this mean.
It means that if I add that no bonuses modify to the beginning.
So for example if I decide that I want to have it sort of in here so we'll have no bonuses modifier.
I'm going to call it the curse that's a curse on the Goblin.
And then I will say a doth add a curse.
Well you're going to see as you execute this whole thing is you're going to see the goblin just being
a one one because all of the subsequent modifiers are only applied if you walk the chain of responsibility.
And what we've done just now is we've added an element which prevents the walking so you see we get
a Goldbloom which is a 1 1 and we are preventing the walking of the chain of responsibility because
it's only done with the space class and location.
Now before we move on to the more sophisticated implementations I just wanted to mention the fact that
this particular implementation of a chain of responsibility is really quite artificial because essentially
what's happening here is you're building a singly linked list so the question is well why not just use
a list or a vector.
It's certainly a very valid concern.
Why not simply just have some sort of structure maybe inside the goblin itself which keeps the different
quantifiers.
So this is how people used to build chain irresponsibilities.
But it's not really how it's done any more.