Monday 27 January 2014

State Design pattern - Real time example [Project]

State Design pattern - Class Diagram

Sunday 26 January 2014

Bridge Design pattern - Implementation [LED TV]



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

Click the below Image to Enlarge

Bridge Design pattern - Implementation [LED TV]



















Bridge Design pattern - Implementation [LED TV] - Class Diagram




























LEDTV.java

public interface LEDTV
{
public void switchOn();

public void switchOff();

public void setChannel(int channelNumber);
}


SamsungLedTv.java

public class SamsungLedTv implements LEDTV
{

@Override
public void switchOn()
{
System.out.println("Turning ON.. Samsung TV.");
}

@Override
public void switchOff()
{
System.out.println("Turning Off.. Samsung TV.");

}

@Override
public void setChannel( int channelNumber )
{
System.out.println("Setting channel Number " + channelNumber + ".. on Samsung TV");

}

}


SonyLedTv.java

public class SonyLedTv implements LEDTV
{

@Override
public void switchOn()
{
System.out.println("Turning ON.. Sony TV.");
}

@Override
public void switchOff()
{
System.out.println("Turning Off.. Sony TV.");

}

@Override
public void setChannel( int channelNumber )
{
System.out.println("Setting channel Number " + channelNumber + ".. on Sony TV");

}

}


AbstractRemoteControl.java

abstract class AbstractRemoteControl
{

protected LEDTV ledTv;

protected AbstractRemoteControl( LEDTV ledTv )
{
this.ledTv = ledTv;
}

public abstract void switchOn();

public abstract void switchOff();

public abstract void setChannel( int channelNumber );

}

SamsungRemoteControl.java

public class SamsungRemoteControl extends AbstractRemoteControl
{

public SamsungRemoteControl( LEDTV ledtv )
{
super(ledtv);
}

@Override
public void switchOn()
{
ledTv.switchOn();
}

@Override
public void switchOff()
{
ledTv.switchOff();

}

@Override
public void setChannel( int channelNumber )
{
ledTv.setChannel(channelNumber);
}

}

SonyRemoteControl.java

public class SonyRemoteControl extends AbstractRemoteControl
{
public SonyRemoteControl( LEDTV ledtv )
{
super(ledtv);
}

@Override
public void switchOn()
{
ledTv.switchOn();
}

@Override
public void switchOff()
{
ledTv.switchOff();

}

@Override
public void setChannel( int channelNumber )
{
ledTv.setChannel(channelNumber);
}
}

Client.java

public class Client
{

public static void main( String[] args )
{

SonyRemoteControl sonyRemoteControl = new SonyRemoteControl(new SonyLedTv());
sonyRemoteControl.switchOn();
sonyRemoteControl.switchOff();
sonyRemoteControl.setChannel(20);
System.out.println("**********************************************************************");
SamsungRemoteControl samsungRemoteControl = new SamsungRemoteControl(new SamsungLedTv());
samsungRemoteControl.switchOn();
samsungRemoteControl.switchOff();
samsungRemoteControl.setChannel(20);
}
}

Output

Turning ON.. Sony TV.
Turning Off.. Sony TV.
Setting channel Number 20.. on Sony TV
**********************************************************************
Turning ON.. Samsung TV.
Turning Off.. Samsung TV.
Setting channel Number 20.. on Samsung TV

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 [Publish Message]
  • Bridge Design pattern - Keypoints
  • All Design Patterns Links
  • 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
  • Saturday 25 January 2014

    State Design pattern - Real time example [Vending Machine]

    State Design pattern - Real time example [TV Remote]

    State Design pattern - Real time example [ATM]

    State Design pattern - Introduction

    State Design pattern - Keypoints

    Bridge Design pattern - Implementation [Send Message]



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

    Click the below Image to Enlarge
    Bridge Design pattern - Implementation [Send Message]














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






















    MessageSender.java


    public interface MessageSender
    {
    public void sendMessage(String message);
    }

    SmsMessageSender.java

    public class SmsMessageSender implements MessageSender
    {
    public void sendMessage(String message)
    {
    System.out.println("'"+message+ "'   : This Message has been sent using SMS");
    }
    }

    EmailMessageSender.java

    public class EmailMessageSender implements MessageSender
    {
    public void sendMessage(String message)
    {
    System.out.println("'"+message+ "'   : This Message has been sent using Email");
    }
    }

    Message.java

    abstract class Message
    {
    protected MessageSender messageSender;

    abstract public void sendMessage(String message);

    }

    ShortMessage.java

    public class ShortMessage extends Message
    {
    public ShortMessage(MessageSender messageSender)
    {
    super.messageSender = messageSender;
    }
    @Override
    public void sendMessage(String message)
    {
    if(message.length()<=5)
    {
    messageSender.sendMessage(message);
    }
    else
    {
    System.out.println("Sorry cannot send the message.....");
    }
    }

    }

    LongMessage.java

    public class LongMessage extends Message
    {

    public LongMessage(MessageSender messageSender)
    {
    super.messageSender = messageSender;
    }

    @Override
    public void sendMessage(String message)
    {
    messageSender.sendMessage(message);
    }

    }

    Client.java

    import java.util.Scanner;

    public class Client
    {

    public static void main(String[] args)
    {

    System.out.println("Do you want to send 'longmessage' or 'shortmessage' ?");
    Scanner scanner = new Scanner(System.in);
    String messageType = scanner.next();

    System.out.println("Please enter the message you want to send");
    Scanner scanner1 = new Scanner(System.in);
    String message = scanner1.nextLine();
    if (messageType.equalsIgnoreCase("longmessage"))
    {
    Message longMessage = new LongMessage(new EmailMessageSender());
    longMessage.sendMessage(message);
    }
    else
    {
    Message shortMessage = new ShortMessage(new SmsMessageSender());
    shortMessage.sendMessage(message);
    }

    }
    }

    Output

    Do you want to send 'longmessage' or 'shortmessage' ?
    longmessage
    Please enter the message you want to send
    Hello how are you ? when did you come?
    'Hello how are you ? when did you come?'   : This Message has been sent using Email

    Do you want to send 'longmessage' or 'shortmessage' ?
    shortmessage
    Please enter the message you want to send
    hi
    'hi'   : This Message has been sent using SMS

    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 [Publish Message]
  • Bridge Design pattern - Implementation [LED TV]
  • Bridge Design pattern - Keypoints
  • All Design Patterns Links
  • Friday 17 January 2014

    Bridge Design pattern - Implementation [Shape]



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

    Click the below Image to Enlarge
    Bridge Design pattern - Implementation [Shape]
















    Bridge Design pattern - Implementation [Shape] - Class Diagram


















    Shape.java


    abstract class Shape
    {

    protected ColorImplementor color;

    protected ColorImplementor getColor()
    {
    return color;
    }

    protected void setColor( ColorImplementor color )
    {
    this.color = color;
    }

    abstract public void colorIt();

    abstract public void draw();
    }

    Circle.java

    public class Circle extends Shape
    {

    public void colorIt()
    {
    System.out.print("Circle filled with ");
    color.fillColor();
    }

    @Override
    public void draw()
    {
    System.out.println("Circle has been drawn with out any color");
    }
    }

    Rectangle.java

    public class Rectangle extends Shape
    {

    public void colorIt()
    {
    System.out.print("Rectangle filled with ");
    color.fillColor();
    }

    @Override
    public void draw()
    {
    System.out.println("Rectangale has been drawn with out any color");
    }
    }

    ColorImplementor.java

    public interface ColorImplementor
    {
    public void fillColor();
    }

    BlueColorImplementor.java

    public class BlueColorImplementor implements ColorImplementor
    {
    public void fillColor()
    {
    System.out.println("blue color using BlueColorImplementor");
    }
    }

    GreenColorImplementor .java

    public class GreenColorImplementor implements ColorImplementor
    {
    public void fillColor()
    {
    System.out.println("green color using GreenColorImplementor");
    }
    }


    BridgeDesignPatternClient.java

    import java.util.Scanner;

    public class BridgeDesignPatternClient
    {

    public static void main( String[] args )
    {

    System.out.println("Please enter the color you want to fill  : 'Green' or 'Blue'");
    Scanner scanner = new Scanner(System.in);
    String color = scanner.next();
    System.out.println("color : " + color);

    Shape rectangleShape = new Rectangle();
    rectangleShape.draw();
    Shape circleShape = new Circle();
    circleShape.draw();
    System.out.println();
    if( "blue".equalsIgnoreCase(color) )
    {
    rectangleShape.setColor(new BlueColorImplementor());
    rectangleShape.colorIt();

    circleShape.setColor(new BlueColorImplementor());
    circleShape.colorIt();

    }
    else if( "green".equalsIgnoreCase(color) )
    {
    rectangleShape.setColor(new GreenColorImplementor());
    rectangleShape.colorIt();

    circleShape.setColor(new GreenColorImplementor());
    circleShape.colorIt();
    }

    }
    }

    Output

    Please enter the color you want to fill  : 'Green' or 'Blue'
    Green
    color : Green
    Rectangale has been drawn with out any color
    Circle has been drawn with out any color

    Rectangle filled with green color using GreenColorImplementor
    Circle filled with green color using GreenColorImplementor


    Please enter the color you want to fill  : 'Green' or 'Blue'
    Blue
    color : Blue
    Rectangale has been drawn with out any color
    Circle has been drawn with out any color

    Rectangle filled with blue color using BlueColorImplementor
    Circle filled with blue color using BlueColorImplementor


    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 [Send Message]
  • Bridge Design pattern - Implementation [Publish Message]
  • Bridge Design pattern - Implementation [LED TV]
  • Bridge Design pattern - Keypoints
  • All Design Patterns Links
  • Bridge Design pattern - Real time example [TV]