All right so one of the classic examples of the mediator design pattern is of course the chat room if
you're going to see a mediator or example in any programming language Somebody is bound to build a chat
room almost certainly So we're going to do exactly the same.
And you say I've put the screen here in half because we're going to have headers and CPD files for once.
I mean I try to keep everything in the single CVP file but this time round it's simply not going to
work.
OK so what I'm going to do is I'm going to first of all define a participant in a chat room that's going
to be a class called Person.
So let's see if we can use some of this nice utility stuff from sea lines to actually generate the class.
I'm going to call this person.
So we're going to have person dot hate and person that CPA which I hope to move here.
I don't like the way that this typically works.
I mean on a day I'll never get used to it.
I mean Visual Studio everything's perfect here it's like a mess.
But anyways we have person Daut CBP and we have person to teach BP and we're going to add some stuff
into it.
I'll use a struct.
As always I just keep everything public for now.
So essentially what we're going to have is we're going to have different participants in a chat room
so they're all going to keep a reference or a pointer to the chat room itself.
That's how it typically goes with a mediator a mediator is something that everybody can reference.
It's either it's some sort of global static variable or something or it's just passed in the constructor
for every single created object and that's probably a better approach because if you think about controlling
object creation then things like dependency injection.
It's just better to stick the mediator as a kind of global service you can even mark it as a singleton
in the dependency injection container which you can find out more about in the singleton part of this
course.
So anyways the person is going to have a name and I guess I need a couple of include.
Let's actually have all the includes in here instead of my hitter's that page you saw include string
and we're going to be using some other stuff I guess so using names face that's not the best practice
in the world but you know for brevity's sake and then I'm going to have a pointer to the chat room chat
room room now.
We are all just forward declare it here it can then declare it using pretty clear.
There we go.
Wonderful except I want to as a struct but never mind.
It's just a semantic difference I guess.
So anyways what I want is I want a constructor which actually takes the name and initializes the name.
So let's have that.
So we'll have a person with just the name itself and then we're going to have a bunch of utility functions
for that kind of stuff that we want.
So for example the person might have a chat log they might have a let's say a vector of strings and
once again we get a vector for this one.
So we're going to have a vector of strings representing the chat log.
And this is where we can add the messages as this person receives them.
And so that when they log out and log back in again they could see their log something to that effect.
So we're going to have three different functions in the person.
So when I have a function called say which is going to allow us to say a particular message and everybody
in the room can hear it so it's kind of like a broadcast operation but I'm going to call it say because
it just sounds better basically.
So that's one.
Now the second thing that we're going to have is we're going to have a function called PM which is short
for private message.
Maybe I should spell it out private message but no let's keep it short considering we've lost half the
screen real estate by doing the split.
So basically here you have to specify who exactly you are messaging.
And then you have to specify the message always that we get.
So that's another function that we're going to implement can also be Konst.
Why not.
So let me generate a body for it in the CBP file.
And finally we'll have a function called receive now receive is not for sending a message but for actually
receiving the message and maybe displaying it on your screen and adding into the chat look so stuff
like that.
So half voyde receive which takes a Origin Origin's describes where we got the message from.
So we could be getting the message from another user for example or we could be getting the message
from the room.
That's when service messages come in.
Like for example the room announces when some of the joins.
So that would be the origin would be set to room.
In this case.
So the other thing is of course the message itself.
So receive it's not a function because you might want to add something to the chat logs so let's actually
generate the definition for forwards as well.
OK so another thing that I'm going to do I'm going to generate is I'm going to generate the quality
comparisons for a person just comparing the name basically.
And that's going to be relevant because when you send a message to the whole room everybody receives
that message but you don't want to call your own receive because that would be kind of duplication you've
already typed out the message.
So it's really it's a stylistic choice.
But I just want to illustrate this basically we're going to get equality operators for just the name
today.
So we simply compare the names.
OK so now onto the implementation because we've got to implement these methods and we also have to build
the chat room.
So maybe that's what we should do.
Maybe we should go ahead and make another class chat room.
OK.
So once again I'll keep it as a struct simply to keep it as a struct.
I'll move those CBP over on the Reuters and.
Oh my God this is so tacky.
I hate this anyway.
Chat Room.
So what are we going to have.
Well first of all we'll have a list of participants.
So the next person star people now person so far away do I want to stick it.
Well I'm not sticking it here.
I'm not for declaring it may be inside the CBP.
I will include verse in that HBP and that's it for now.
This becomes about it.
OK.
So we have a list of people in the room and then we want to implement certain operations inside the
chat room one of those operations is to allow anyone who has a pointer to the chat room or indeed reference
to broadcast the message.
So we're going to call this broadcast so once again origin and the second is the message.
Oh right.
So let's implement this actually generate the body and then inside the body what we're going to do is
we're going to go through every single person inside the inside the room except the one that generate
the message and we going to broadcast we're going to get them to receive the message basically.
So for auto pee in people.
So for every single person if the persons thing is not equal to origin actually we could begin comparing.
Well actually no this is this is OK I'm thinking that maybe I wasted some time by implementing the quality
stuff.
So here provided we're not the source of the message.
We are the one who came in we called P-doc received so everybody else gets the message.
And so the source's origin or you could specify a room for example and the message is message like that.
So that's for joining now.
The second thing that we're going to implement so that's for broadcasting rather the thing for joining
is the effort so avoid join.
So here some person joins the room like so what we're going to do is we were going to basically define
the message so we'll generate the message.
Let's actually use string join Amnesty so we can have the name of the person and the words joins the
chat.
Hey we're running out of space finally.
OK so now what we do is we broadcast the message
the source's room because that's joined message it's kind of a public service announcement and we broadcast
join them as G.
And so another thing that we do once the person joins is we set a room point to remember if we go back
to a person that they should be they have this chat room point so we said it here we set up the room
equals this and we have this person to the list of participants in the room.
So we say people don't push back.
P OK.
So the last thing that we need to do is we need to implement private message inside the chat room.
So for this what we're going to do is we're going to implement a function called message which is going
to take the origin that's where we are who actually is sending the message.
We specify who the message is for and we specify what the message actually is.
OK so first of all what we need to do is we need to find the person with the right name because we're
specifying the name here who is a string and we need to find the string in a list of people.
Obviously So here to target equals fine if.
So we need algorithms for this.
Now she stick it in here we go.
OK.
So find if for me it's taking some result but that's because I put it in the wrong place.
It happens it happens.
Stay here.
It still we find if so we are looking in again people and people.
OK.
So what we want to find is we want to find some pointer.
We want to find some pointer where the name matches so return.
The name is equal to who is the target.
OK.
If we find the target so if Target.
By the way after another round bracket here.
So if Target is not equal to and people then we found someone we found someone to send the message to.
And so we take the target.
We follow the other iterators those are targets and then gets us the pointers to them we do reference
the point here and we call receive.
So the person sending the message is origin and the message is message.
OK so this is the implementation of the chat remember can now go back to person B and person the CBP
and finally complete the implementation.
So person got CBP.
Let's actually take a look at it here so let's start with the safe function.
So when you say something you broadcast it to the room and we have a pointer to the room so we say a
room broadcast or the source is our own name and the message is message that we have broadcast.
I think we.
Wait wait a second.
Actually verify this so broadcast.
Definitely do.
So it makes me wonder what's going on here.
Let's actually leave it like this for now.
Well if something is wrong we'll figure it out eventually.
I wonder why so.
Chat room does not have a function called broadcast.
Yeah right here.
This is definitely there.
OK so the second thing is private messaging so private messaging is done using the message function.
So once again room message OK.
Stoning rather again for some reason so name who message fairly obvious stuff.
And finally receiving the message that's where we can actually displayed on the screen and also do some
of the other stuff.
So let's have.
Oh well let's actually specify who's chat session.
It is we are in flustering as origin Plus a colon and the quote message and then closing quote just
so that we get to see the message in full then we see out whose session it is so see it.
The VC out I guess.
Wait wait a second don't we have IO stream somewhere.
Let's take a look we have person.
Oh I just realized why we are not getting those help just because we haven't include a chat room.
So once this is done you'll notice all the red stuff is gone and well I guess we we still need a stream
somewhere so that we stick it in here.
Not very imaginative I know soci out on the opening square brackets in the name and then specify that
it's their chat session chat session like so and then it's out for the string s and a line break on
the end and then we can also add it to the chat logs.
Stick it into the chat log as well.
So that's all that we're doing here.
So we're getting the participant to refer to the chat room and the chat room is it's kind of like the
glue but nobody has a direct reference to somebody else.
You'll notice that a person does not have any pointers to other people and it doesn't use.
I mean typically what you would do is in the chat room you would actually make this private you make
the set of people private so that nobody could manipulate it somehow because it doesn't make any sense.
But it doesn't really matter in this particular implementation I think we're done for the most part
and what we can do now is we can actually try and run all of this.
So let's get some includes in here some good person.
And I'm going to need a chat room not HBP as well.
And then what we're going to do is we're going to make some people and just generate a chat session.
So here is the chat room
shot north Chaar chat room.
I will going to have two people John and Jane to begin with.
And John John and Jane
Jane that will go OK.
So they're both going to join the room.
So run that join John and join Jane.
They both get to Jane.
Join the room and then they get to say things so John can say hi room.
And then Jane Jane can say Oh hey John
I think we can also have another person join in so we can have a person called Simon
Simon so room join Simon like so and then Simon says Hi everyone.
Well let's also try other private message functionalities.
Jane is going to private message Simon and she is going to say glad you found us.
OK.
So we've got this scenario.
Let's actually run this and see what we get here.
All right so we got the chat.
Let's actually take a look at it and I'm going to bring up the the original scrip here.
So when Kyon joins the room there is no message generated because there's nobody for this message to
there's nobody to see this message effectively.
But when Jane joins the room because John is already in here his chat session here says Jane joins the
chat.
And then John says hi room.
And Jane sees it and well Joan sees it as well because we've chosen to replicate the messages across.
And then Jane says oh hey John.
And there there as well.
And then Simon joins the chat.
And both John and Jane see it in their chat session.
And then Simon says Hi everyone and both John and Jane see it in their chat session.
And then here is the private message.
So Simon's chat session is the only session where Jane says Glad you found this job.
So this is how you implement a simple chat room and indeed you implement the mediator design pattern
so the takeaway from this is that you have a central component in this case it's the chat room and every
single part of the system has a reference or appointed to that construct and they all communicate exclusively
through that point or so they don't communicate directly.
They don't have any references or pointers to one another but they can send messages for example in
this case I'm using the name of a person has the kind of key for actually message passing and the chat
room is the mediator who actually takes care of the glue.
The thing which which kind of binds everything together.