Thursday, 26 December 2013

Mediator Design pattern - Implementation [Facebook]



Click here to watch in Youtube : https://www.youtube.com/watch?v=bayJB37kVR0

Click the below Image to Enlarge
Mediator Design pattern - Implementation [Facebook]

Mediator Design pattern - Implementation [Facebook] - Class Diagram




FacebookGroupMediator.java

public interface FacebookGroupMediator
{

public void sendMessage( String msg, User user );

void registerUser( User user );
}


FacebookGroupMediatorImpl.java

import java.util.ArrayList;
import java.util.List;

public class FacebookGroupMediatorImpl implements FacebookGroupMediator
{

private List<User> users;

public FacebookGroupMediatorImpl()
{
this.users = new ArrayList<User>();
}

@Override
public void registerUser( User user )
{
this.users.add(user);
}

@Override
public void sendMessage( String msg, User user )
{
for( User u : this.users )
{
// message should not be received by the user sending it
if( u != user )
{
u.receive(msg);
}
}
}

}

User.java

public abstract class User
{
protected FacebookGroupMediator mediator;
protected String       name;

public User( FacebookGroupMediator mediator, String name )
{
this.mediator = mediator;
this.name = name;
}

public abstract void send( String msg );

public abstract void receive( String msg );
}

UserImpl.java

public class UserImpl extends User
{

public UserImpl( FacebookGroupMediator mediator, String name )
{
super(mediator, name);
}

@Override
public void send( String msg )
{
System.out.println(this.name + ": Sending Message=" + msg+"\n");
mediator.sendMessage(msg, this);
}

@Override
public void receive( String msg )
{
System.out.println(this.name + ": Received Message:" + msg);
}

}

FacebookClient.java

public class FacebookClient
{

public static void main( String[] args )
{
FacebookGroupMediator facebookMediator = new FacebookGroupMediatorImpl();
User dave = new UserImpl(facebookMediator, "Dave");
User ram = new UserImpl(facebookMediator, "Ram");
User raj = new UserImpl(facebookMediator, "Raj");
User anu = new UserImpl(facebookMediator, "Anu");
User rahul = new UserImpl(facebookMediator, "Rahul");
User steve = new UserImpl(facebookMediator, "Steve");
User raju = new UserImpl(facebookMediator, "Raju");

facebookMediator.registerUser(dave);
facebookMediator.registerUser(ram);
facebookMediator.registerUser(raj);
facebookMediator.registerUser(anu);
facebookMediator.registerUser(rahul);
facebookMediator.registerUser(steve);
facebookMediator.registerUser(raju);
dave.send("ramj2ee.blogspot.com - this blog is very good blog ");
System.out.println("##############################################################################");
ram.send("what is Design Patterns? Please explain ");

}

}


Output

Dave: Sending Message=ramj2ee.blogspot.com - this blog is very good blog 

Ram: Received Message:ramj2ee.blogspot.com - this blog is very good blog 
Raj: Received Message:ramj2ee.blogspot.com - this blog is very good blog 
Anu: Received Message:ramj2ee.blogspot.com - this blog is very good blog 
Rahul: Received Message:ramj2ee.blogspot.com - this blog is very good blog 
Steve: Received Message:ramj2ee.blogspot.com - this blog is very good blog 
Raju: Received Message:ramj2ee.blogspot.com - this blog is very good blog 
##############################################################################
Ram: Sending Message=what is Design Patterns? Please explain 

Dave: Received Message:what is Design Patterns? Please explain 
Raj: Received Message:what is Design Patterns? Please explain 
Anu: Received Message:what is Design Patterns? Please explain 
Rahul: Received Message:what is Design Patterns? Please explain 
Steve: Received Message:what is Design Patterns? Please explain 
Raju: Received Message:what is Design Patterns? Please explain 

See also:

  • Mediator Design pattern - Introduction
  • Mediator Design pattern - When to Use
  • Mediator Design pattern - Real Time Example [Facebook]
  • Mediator Design pattern - Real Time Example [ATC]
  • Mediator Design pattern - Real Time Example [Chat Room]
  • Mediator Design pattern - Class Diagram
  • Mediator Design pattern - Implementation [Chat Room]
  • Mediator Design pattern - KeyPoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge