Mediator and Command Pattern

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

  1. Command is the core of command design pattern that defines the contract or interface for all the command classes.
  2. Receiver implementation is separate from the command implementation.
  3. 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.
  4. 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.
  5. 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!


要查看或添加评论,请登录

Farruh Habibullaev的更多文章

  • Good API architecture principles

    Good API architecture principles

    Claimer: This article is purely written from my own experience in software development and design, however in some…

  • Practical Dijkstra's Algorithm

    Practical Dijkstra's Algorithm

    In computer science and discrete mathematics, we have encountered the concept of "single - source shortest path" many…

  • Android Core: Looper, Handler, and HandlerThread

    Android Core: Looper, Handler, and HandlerThread

    I have posted this article for those who don't have any idea how multi-threading architecture in android works, and…

  • OkHttp's Ins and Outs (Part 2)

    OkHttp's Ins and Outs (Part 2)

    This is the second part of OkHttp tutorial. You can find the first part of tutorial in the link here.

    1 条评论
  • OkHttp's Ins and Outs (Part 1)

    OkHttp's Ins and Outs (Part 1)

    This is the first part of OkHttp tutorial. 1.

    4 条评论
  • Creational Design Patterns

    Creational Design Patterns

    Creational design patterns are one of three categories of design patterns (creational, structural, and behavioral…

  • Decorator and Proxy Pattern

    Decorator and Proxy Pattern

    Decorator pattern Decorator pattern is one of the structural design patterns and used to modify the functionality of…

  • S.O.L.I.D Principles in OO programming

    S.O.L.I.D Principles in OO programming

    SOLID is the first five essential principles of object oriented programming for building a well designed, working…

  • Composite Design Pattern

    Composite Design Pattern

    Composite design patterns falls into one of structural design patterns. Before diving into the composite design…

    2 条评论
  • Iterator Design Pattern

    Iterator Design Pattern

    Iterator design pattern falls into the category of behavioral pattern. Iterator pattern is used to provide iteration…

社区洞察