This week, I present to you not much used, but quite useful Mediator pattern.

Mediator pattern provides a unified interface to set of interfaces in a subsystem.

Or, to simplify… Mediator pattern is mediator for communication between several classes.

Example

Now, the easiest way to explain this pattern is via example. Example itself is borrowed from dofactory page, but modified to fit LotusScript. It is about a simple support chat room. This chat room has several users of two types: admins and customers. I will call them participants. These participants can communicate amongst each other, presuming, they know user ids.

First, we need a chatroom template class, as we might end up with more than one chat room. Chat room will need to register participants and to be able to deliver messages to them.

As participants need instance of chatroom class and chatroom needs list of participant class instances, we need to create chatroom and participant class frameworks and only then put code into it. Participants need to be able to send and receive messages. Finished classes are displayed below.

Now that base classes are set, it is time to create real participant classes. In our case, we need one for admins and one for regular users.

Testing the code

For testing purposes, I put it all in agent, created four users (of which one is admin) and sent 3 messages to admin and admin responded to first one. The code is below.