Lecture thumbnail 0:00 / 0:00 A classic example of the mediator design pattern is a chatroom, so why not build one?
Let’s actually build a very simple chat room.
So a chat room is composed of several people all interacting.
So we’re going to begin by making a class called person.
I’ll have that person have a name, but in addition, the person is also going to have a reference to
the room that they’re currently in.
So we’re going to have public chat room room like so.
And of course we have to create the chat room class.
So let’s create that.
Now, in addition, the person is going to have a chat log, so the chat log is basically going to be
just a list of strings representing all the messages that this particular person has received.
So we can have a private list of string called chat log and that’s going to be initialized like so let’s
go again.
There we go.
Okay, so now that we have this, let’s make a constructor, maybe a constructor which just initializes
the name.
So generate code constructor.
Initialize the name.
There we go.
Don’t care about the null checking part, and then we have a bunch of APIs for what the person can do.
So for example, they can say something and that means they are broadcasting their message to the room.
So that’s going to be a message.
In addition, what they can do is they can send a private message, private message.
And in this case, we have to specify who the private message is to and what the message is.
So finally they can receive a message.
And in this case, we might want to have some diagnostic output.
So whenever a person receives a message, we’re going to write it to the console.
So we’ll have string sender and string message.
And here we’re going to well, we’re going to do a couple of things.
First of all, we’ll make the actual message so that we can add it to the chat log.
So I’m going to say string S equals and then we’ll get the sender colon and then the message in single
quotes like so I will add this message to the chat log, chat, log, dot, add s, but in addition,
we’ll write line what it actually is and we’re going to write line this, including the information
about whose chat session it actually is.
So we’re going to say this is names chat session and then we’ll put the message in here.
So that’s our diagnostic information.
Now, in order to say something or to send a private message, we need a reference to the chat room
and we haven’t built the chat room yet, so let’s actually do that now.
So the chat room is just going to be a list of people.
So private list of person people equals new.
There we go.
So this is the set of people talking in the chat room, and then we’ll have a bunch of methods for,
first of all, joining so a person can join the room.
So public void join person P and here we’re going to have the join message string, join message equals
and then we’re going to have P name P dot name joins the chat we’re going to broadcast.
This message to the entire room, because as soon as somebody joins, everybody needs to be notified.
So the target is going to be room and we’re going to send join message.
And by the way, we don’t have the broadcast method yet, so let’s make that as well.
So here we’ll have a string representing the source.
And actually, no, that’s that’s not right.
The string represents the.
Yeah, yeah.
It’s going to be the source because it’s the room telling everybody that somebody has joined source
and then the string is going to be message like so so the idea of broadcast is you go through every
single person and you actually tell them that this message has come in.
So this is where the chat room is mediating the messages.
So it’s kind of like the central message hub.
Somebody broadcasts a message, even if it’s the room itself and you have to send it to everyone in
the room.
So for this we do for each var p in people, we check that the name of the person is not the same as
the source because if you broadcast, you don’t want it to be replayed to yourself.
So if p dot name is not equal to source, then we say p dot receive is receive public.
Let’s take a look.
If we go up into person for a moment.
Yeah, it’s a public thing so p dot receive and here we have the source as well as the message.
There we go.
So this is how a room can actually be used to broadcast a message to every single participant in chat.
And finally, we’re going to have a private message.
So we’re going to have a method called message where you specify the source, who’s sending the message,
you specify the destination, and you specify the message, obviously.
So in this case, what we do is we say we find the person for whom this message is addressed because
that’s the destination string and we’re keeping people as a list of persons.
So we have to do a search.
We do people dot first or default where p dot name is equal to the destination.
And then what we do is we do a safe call because remember, I’m calling first or default.
So that can return null if we don’t find the person.
So I’m doing a save call to receive with source and message.
There we go.
So in our scenario, the chat room is a central component which acts as a mediator and which allows
people to communicate with one another without really knowing that they’re present.
So, for example, when you say something, you’re telling it to the whole room.
So we can now finish off the implementation of say and private message.
So in the case of say, we say room dot, broadcast, broadcast and we specify our own name as the source
and the message.
So that’s easy.
And by the way, that probably has to be made public if it hasn’t been made public already.
And for the private message, we have to.
Well, it’s also easy.
So we say room dot message and we say the source is us.
So our name, we message who.
And here is the message itself.
Okay, so having built this mediator, what we can now do is we can try it out and see what the room
messages are.
So we’re going to make the room itself.
New chat room.
In addition, we’re going to make the room participants like John, which is going to be a new person
called John like.
So now replicate this and I’ll have Jane as well.
Jane like so.
And then we’re going to have both of them join the room.
So John Dot join.
Actually it’s room dot, join John and the same goes for Jane.
So both of them join the room and then they can start talking to one another.
So for example, John can say hi, for example, or Jane can say.
Oh, hey, John.
There we go.
And then we can have someone else join in.
Like var Simon equals new person.
Simon Simon’s going to join the room.
So room dot, join Simon.
And then, well, let’s say Simon.
Simon says, Hi, everyone.
And then Jane sends him a private message so Jane can send a private message to Simon.
And the private message says.
Glad you could join us.
Exclamation mark.
There we go.
Okay.
So now that we’ve built this scenario for our mediator, let’s actually execute this and see what happens.
And we’re getting an exception to begin with.
So let’s let’s figure that out, first of all, because that is slightly unpleasant, shall we say.
So I’m going to close this and we’re going to just F5 it and see where this thing crashes because,
well, it has to has to crash somewhere.
All right.
So we probably forgot to initialize the room.
Yeah.
So we forgot to initialize the room.
And this is something that you can do in the join.
So if we go to the join method, we can add a few more functionality because we forgot to actually add
the person.
So the person’s room is set to this and then we add it to the set of people.
So we say people dot add P, there we go.
So second time lucky hopefully, and we get our chat log.
And as you can see here, what’s happening is remember we have John joining, but when John joins,
there is no message being sent to anyone because he’s the only person in the room.
There’s nobody else in the room.
So no messages are sent.
But when Jane joins the chat, then John receives that chat message because he’s already in the room.
And then John says hi.
So Jane gets that.
John obviously doesn’t because he’s the one who said it effectively.
And then John’s chat session gets Jane’s message.
Oh, hey, John.
Now Simon joins the chat and both John and Jane’s chat sessions get the notification that Simon joins
the chat and Simon says, Hi everyone, and both John and Jane get the message.
And then here is the private message from Jane to Simon.
So it’s in the Simon In Simon’s chat session, Jane says, Glad you could join us.
So as you can see, we’ve built a chat room which acts as a mediator between different people, and
the people don’t have to be aware of one another beforehand, meaning they don’t have to have references
to one another.
They can refer to each other by name if they want to.
They can get a list of all the participants in the room.
They can send messages.
And if somebody tries to send a message to somebody that doesn’t exist, like if we make a mistake here
and nothing bad is going to happen because we essentially have this safe access here.
So if you don’t find the person to send the message to you, simply don’t send the message.
It’s as simple as that.
And this is what makes the mediator so great.
It’s the fact that you don’t have to be aware of the other participants.
And if some of the participants drop out, your messages are still valid.
They just don’t go anywhere.
Play Stop Play Play Start Play information alert