We're I'm going to take a look at something called an event a broker and in actual fact I've already
shown in the RENNEBERG as part of the chain of responsibility those I'm hired by now we're going to
do it again because when we talked about change responsibility we'll look specifically about building
sequences of objects which get to process a particular happening one after another whereas here all
we're really interested in is we're interested in just handing events in a centralized manner.
And this centralization is going to be fleshed out with the use of the observer pattern and the Mediator
pattern.
So here's the idea let's imagine that you have a football game going on so the football game is you've
got a couple of players and you also got the football coach.
And let's suppose that the football coach wants to be notified of situations when the player scores
go because the football coach wants to.
I didn't jump out onto a field or clap or say something nice to the player for example.
So we're going to simulate this whole thing now in order to actually get the notifications about scoring
goals.
For example I'm using Boost signals too so that's a signal library that we're going to be using to actually
generate the events.
So let's let's begin somewhere so we're going to have a structure called the game.
So a game is essentially you know you get a bunch of players running around or whatever.
Actually not going to even keep pointing references to them because all we're interested in in things
which happen in the game.
So I make a signal which is going to post some event data.
So we're going to be essentially generating different events in the game and the football coach for
example might want to receive those events and act on them events that week.
OK so I'm going to need obviously to have a structure called event data.
So let's define one.
So even data is going to have just a single function called print.
And that's that's going to allow us to actually print whatever event has occurred.
So every single event is going to be printable or we'll be able to print itself basically.
OK.
So what are we going oh wait signal is red.
That means I need it it's another using namespace boost signals to hopefully fix that particular problem.
OK.
So we have the data and we're posting different kinds of event data and that type of event data might
be the player who scored.
So for example we'll have a player will have a player score data and there's going to be data and here
we'll have some information about who scored and how many goals they scored already so I'll have the
player name and I'll have goals scored so far.
OK so let's actually Jerramy the constructor which initializes both of these and let's also implement
for the print function.
So we get to just do a very simple implementation where we print something out to the command line so
see out I'm going to say player name has scored and it's there first second whatever go there goals
scored so far.
Can I please have some code completion here perchance it's actually taking its sweet time goes so far.
Go and then Lindborg.
That's it.
So that's how this particular event is going to print itself.
Now of course what we need to do is we need to get the actual player in here.
So that's actually defined it somewhere down below.
Now remember we have game as the mediator so every single player is going to have a reference to the
game the football coach is going to have a reference to the game because that's how mediator's work
typically.
So we'll have a structure called player so we'll play a name we'll have the number of goals goals scored
so far starting at zero and they'll have a reference to the game.
OK.
So we're going to initialize the object.
I just want to define the name and the game reference nothing else.
So that's going to be our constructor and then we'll have a function for scoring so we're going to simulate
them scoring a goal so we increment the number of goals scored
then we generate the data to be possed posted on the signal.
So we'll have close quarter data.
P.S. where you specify the name of the player as well as the number of goals they scored so far and
then we take game events and we actually post the event.
So.
Yes.
So.
All right so that's one player scoring a goal and then we're going to have the football coach.
So the football coach is actually interested in looking at the players and congratulating them for example
on scoring a goal.
So we're going to have a structure called Coach which is going to take a reference to the game.
It doesn't actually need to keep that reference because we're going to do everything in the constructor.
So let me generate a constructor where we kind of assign again.
So the idea is as follows We take the game we take events stream.
Basically the signal which generates all the events and we say OK can we please connect to it.
So we're going to connect to it and we're going to provide a lambda for handling any kind of event data
event data.
Now remember we're interested specifically in the play scored data.
So this is polymorphic.
You might have other kinds of events and here let me actually on this.
So what we're going to add here is we're going to have basically just a dynamic cast we're going to
check whether we have player squalled data player scored data P.S. equals dynamic cast player scored
data.
So if the cast succeeds and the number of goals scored so far is less than 3 then we can congratulate
the player because if they score three goals in a row Well clearly the other team is outmatched.
So we're going to see out.
Coach says well done.
And then I need the quotes obviously.
So coaches.
Well done.
And then.
Yes.
Player Name exclamation mark Weinberg you know.
So that's all there is to it.
So essentially the coach in the constructor uses the game reference to subscribe to these events happening
in the game and the checks for that particular event and checks that the number of goals scored is less
than three.
And if it is then that congratulates the player.
So that's pretty much it.
I think we missing four with Declaration up here somewhere so maybe that action here I believe and we're
done we're good to go we can actually just experiment with this scenario.
So here's the idea.
We have the game.
That's our event broker and we have a player let's call him Sam with a reference to the game of course
and we also have the coach.
The reference to the game as well.
And now what we can do is we can simulate the player scoring three times in a row.
So we should only get the congratulation message twice because on the third one the player is the coach
rather Isn't that pressed anymore.
So let's execute this and see what we get.
All right so as you can see if we go to exactly what we wanted so the player scores twice.
When the player scores what happens is we generate an event we generate effectively a call on the signal
and every single subscriber gets gets the invocation.
So in this case what happened in the coaches we supplied this lambda function to be invoked every time
somebody generates the signal.
And so it has been invoked twice because on the third one the coach doesn't really care anymore.
So this is a more sophisticated implementation of the mediated design pattern coupled together with
the observer having to create something called an event Broecker.