Mediator and Command Pattern
Mediator Pattern
Mediator pattern falls into category of behavioral patterns since it deals with behavior of the objects. It provides the central communication medium for the objects in the application to interact with one another.
According to GoF, the mediator design pattern allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other, and allows objects of each set vary independently of one another.
The mediator pattern plays an important role in the large scale of enterprise application where multiple objects need to interact with each others. If we allow those objects to interact with each other directly, they form tight coupling, causing high maintenance cost, and hard to extend.
In the following case, mediator pattern is used:
- Mediator pattern is used to reduce the communication complexity between the multiple objects. The pattern provides a mediator object that handles all the communication between different objects.
- Mediator is a communication center for the objects. When the object needs to communicate with another object, it does not call the other object directly. Instead, it calls the mediator object whose main duty is to route the message to the destination object.
Imagine the scenario where we have ten thousands of objects that want to communicate with one another, and all the objects can call and/or send messages to each other, and thus know about each others, causing the tight coupling.
Another real time example, in facebook, we can create a specific group "algorithmic thinking" where users can join and share the information. Here the group is playing a role of mediator where it receives information and routes to relevant users.
Here is the simple java implementation in my github here
Another example would be the ATC Mediator in which the pilots of plane departing and approaching on the terminal area communicate with ATC mediator rather than communicating with one another. The constraints who can take off or land are enforced by the tower. It is important to note that the tower does not control the whole flight. It is there only to enforce the constrains on terminal area.
Most obvious example here is the users can do the chat group. Every users will be identified by name, and they can send and receive a message. The message sent by users would be received by any other users in the group.
You can find the sample java chat app application in my github here
Command Pattern
Command pattern falls under the behavioral pattern. The request is sent to invoker and the invoker pass the request to an encapsulated command object, then the command object passes the request to the appropriate method of Receiver to perform a specific action.
The client creates the receiver object and attaches the command object. Then it creates the invoker object, then attaches the command objects to it to perform an action.
Now the client executes an action, it is processed based on command and receiver object.
Let us consider a simple example which we can implement the command pattern. Assume we would like to implement file system where the client can save, open and close the files. To implement the file utility system, first of all we need to create the receiver object that will actually do all the work.
You can find the real time implementation of the following system in my github
In the following case, the command pattern is used:
- Command pattern is used to encapsulate the request as an object (Command) and pass to an invoker wherein the invoker does not know how to serve the request but uses the encapsulated command to perform an action.
In real time example of command pattern is that command patterns allows requests to be encapsulated as objects, thereby allowing the clients to be parametrized with different requests. The check at the diner is an example of a Command pattern. The server takes the order/command from customers and encapsulate that order writing on the check. The check is queued for short order cook. You can realize that the pad of checks used by waiter is independent on the menu. Therefore, they can support the commands to cook many different items.
In summary I would like to mention core points in the command design pattern
- Command is the core of command design pattern that defines the contract or interface for all the command classes.
- Receiver implementation is separate from the command implementation.
- The command implementation classes picks the methods available in the receiver object, and for each method in the receiver there will be one command implementation.
- Invokers class delegates the request from the client to command object. The client is responsible for creating the receiver and command object and associate them together and attach them to invoker.
- Command design pattern is easily extendible and we can add the new action methods in receivers, and create new command implementation without changing the client code.
One of the drawbacks with command pattern is that the code get huge and confusing with high number of action methods, and because of some many associations.
Thanks for reading!