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