Sunday, 26 January 2014

Bridge Design pattern - Implementation [Publish Message]



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

Click the below Image to Enlarge

Bridge Design pattern - Implementation [Publish Message]

Bridge Design pattern - Implementation [Publish Message] - Class Diagram


MessagePublisher.java

public interface MessagePublisher
{
public void publishMessage(String message);
}


Facebook.java

public class Facebook implements MessagePublisher
{

public void publishMessage(String message)
{
System.out.println("'"+message+ "' : This message has been published to public in Facebook Social networking site");
}
}

Twitter.java

public class Twitter implements MessagePublisher
{

public void publishMessage(String message)
{
System.out.println("'"+message+ "' : This message has been published to public in Twitter Social networking site");
}
}

GooglePlus.java

public class GooglePlus implements MessagePublisher
{

public void publishMessage(String message)
{
System.out.println("'"+message+ "' : This message has been published to public in GooglePlus Social networking site");
}
}


Message.java

abstract class Message
{

protected MessagePublisher messagePublisher;

protected MessagePublisher getMessagePublisher()
{
return messagePublisher;
}

protected void setMessagePublisher( MessagePublisher messagePublisher )
{
this.messagePublisher = messagePublisher;
}

abstract public void publishMessage(String message);

}

LongMessage.java

public class LongMessage extends Message
{

@Override
public void publishMessage( String message )
{
messagePublisher.publishMessage(message);
}

}

ShortMessage.java

public class ShortMessage extends Message
{

private static int MESSAGE_LENGTH = 200;

@Override
public void publishMessage( String message )
{
if( message.length() <= MESSAGE_LENGTH )
{
messagePublisher.publishMessage(message);
}
else
{
System.out.println("Sorry message length is more cann't publish.....");
}

}

}


Client.java

import java.util.Scanner;

public class Client
{

public static void main( String[] args )
{

System.out.println("Please enter the Social networking site you want to use to publish the message  : 'Twitter' or 'Facebook' or 'GooglePlus'");
Scanner scanner = new Scanner(System.in);
String socialNetworkingSite = scanner.next();
System.out.println("Please enter the message you want to publish");
Scanner scanner1 = new Scanner(System.in);
String message = scanner1.nextLine();

if( "Twitter".equalsIgnoreCase(socialNetworkingSite) )
{
ShortMessage shortMessage = new ShortMessage();
shortMessage.setMessagePublisher(new Twitter());
shortMessage.publishMessage(message);
}
else if( "Facebook".equalsIgnoreCase(socialNetworkingSite) )
{
LongMessage longMessage = new LongMessage();
longMessage.setMessagePublisher(new Facebook());
longMessage.publishMessage(message);
}
else if( "GooglePlus".equalsIgnoreCase(socialNetworkingSite) )
{
LongMessage longMessage = new LongMessage();
longMessage.setMessagePublisher(new GooglePlus());
longMessage.publishMessage(message);
}

}
}


Output

Please enter the Social networking site you want to use to publish the message  : 'Twitter' or 'Facebook' or 'GooglePlus'
Twitter
Please enter the message you want to publish
hello
'hello' : This message has been published to public in Twitter Social networking site


Please enter the Social networking site you want to use to publish the message  : 'Twitter' or 'Facebook' or 'GooglePlus'
Facebook
Please enter the message you want to publish
hello bow are you?
'hello bow are you?' : This message has been published to public in Facebook Social networking site

See also:

  • Bridge Design pattern - Introduction
  • Bridge Design pattern - Real time example [Send Message]
  • Bridge Design pattern - Real time example [Publish Message]
  • Bridge Design pattern - Real time example [Shape]
  • Bridge Design pattern - Real time example [TV]
  • Bridge Design pattern - Class Diagram
  • Bridge Design pattern - Implementation [Shape]
  • Bridge Design pattern - Implementation [Send Message]
  • Bridge Design pattern - Implementation [LED TV]
  • Bridge Design pattern - Keypoints
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge