Skip to main content

Mediator Design Pattern


Type : Behavioral Design Pattern

Summary : 

Mediator Design pattern provides a common a platform for communication between different objects.
It promotes loose coupling between interacting objects. In a complex software where number of Classes interacting with each other are more, using Mediator Design pattern is very useful. As Mediator will reduce the complexity  of objects interacting with each other to a centralized Mediator, which results in easy maintenance of code.

Advantages :

Centralizes the interaction between many objects which is easy to maintain.
Promotes decoupling the interacting objects.

Participants :

Mediator
Concrete Mediator
Colleague
Concrete Colleague 

Example :

For our example,  we will consider the example of playing week's most popular new release on home theater entertainment system. We will consider that playing a movie depends on a number of systerms\services.

Let's assume the followings,
We have a website which gives us the name of the most popular new release.
We have a steaming service which can play the Movies
We have a home theater, that is TV+Music System.

We we consider the following classes like TV:turnonTV  Homethreater:turnonHomeTheater, Website:getMostPopularMovieOfWeek and TV:getMovie(Movietitle)

It is clear that many classes and different methods need's to be invoked in order to start the show.

We can have a mediator like following.


StartShowMediator{
startMovie{
TV::turnOn()
HomeTheater::turnon();
Movie movieTitle = IMDB:getWeeklyPopularMovie();
Movie playMovie = NetFlix::getMovie(movieTitle);

if(!playMovie) playMovie = AmaxonStreaming::getMovie(movieTitle);

HomeTheater:playMovie(playMovie);
}
}


This example demonstrates how the introduction of Mediator simplifies the interaction between different classes. For instance later if we want to Change the Media source from NetFlix to Something else, we just need to update the StartShowMediator, without or with very minimum code changes elsewhere.





Comments

Popular posts from this blog

Converting Java Map to String

Java Collections framework, String manipulation etc is something that we often encounter in Development process.
For processing collections (like checking null/empty, Intersection, Disjunction) We do have some of the very use full libraries.

Some of the Collection related libraries are Apche Commons Collections and Google  Collections(Guava).

Problem Use Case

This article explains how to convert a Java Map to String(and vice versa) using different libraries and technique.

One way is to use StringBuilder(Or String) and loop though the Map and build the String by applying some sort of separator ( for key:value and entry). Here we have to take care of the null value etc.

Without Any Library
If we want to convert the map to a String with key value separator and also individual entry seperator in the resulting String, we have to write code for that. For a simple Map, we have to iterate though the map, take care of the null values etc. Following is a sample to get String built out from Map C…

Masking Credit Card number in Java

Sometimes we need to mask crucial information like Credit Card Numbers, CVV numbers etc before storing  or logging the information. This example mask Credit Card Number (Except last 4 Digit) from a Text which contains information along with Credit Card Number.

The following example demonstrates how we can easily mask the credit card with Matcher and Pattern Classes. This Sample Code uses Matcher and Pattern.
Pattern Used in this sample is not optimized for Credit Card Numbers, this pattern will get any numerical numbers in the String Content.  Based on the Credit Card Type a more efficient and Strict RegEx can be used to mask the Credit Card.
/**Mask the Credit card number but last four digit value **/   Pattern PATTERN = Pattern.compile("[0-9]+"); String message = content; Matcher matcher = PATTERN.matcher(message); String maskingChar = "*"; StringBuilder finalMask = new StringBuilder(maskingChar); while …

Invoking EJB deployed on a remote machine

Invoking EJB deployed on a remote machineIn case we are calling remote ejb( ejb deployed on remote machines),The JNDI lookup might lookup like,Properties env = new Properties();env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");env.put(Context.PROVIDER_URL, "XX.XXX.XX.XX:1099");env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); Context ctx = new InitialContext(env);If we are calling local ejb then we can simply create InitialContext without any parameters.Like,Context ctx = new InitialContext();