Skip to content
Open
Next Next commit
Bridge design pattern - BEFORE CODE
  • Loading branch information
premaseem committed Feb 22, 2018
commit b9fda5fc36de88bf8521402e12dddb0949af6385
68 changes: 68 additions & 0 deletions pattern/src/com/premaseem/BridgeRemoteControl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public abstract class BridgeRemoteControl {

private ITV tv;

public BridgeRemoteControl (ITV tv) {
this.tv = tv;
}

public void turnOn () {
tv.on();
}

public void turnOff () {
tv.off();
}

public void setChannel (int channel) {
tv.switchChannel(channel);
}
}

class LogitechRemoteControl extends BridgeRemoteControl {
public LogitechRemoteControl (ITV tv) {
super(tv);
}

public void setChannelKeyboard (int channel) {
setChannel(channel);
System.out.println("Logitech use keyword to set channel.");
}
}

class IndependentRemoteControl extends BridgeRemoteControl {
public IndependentRemoteControl (ITV tv) {
super(tv);
}

public void setChannelKeyboard (int channel) {
setChannel(channel);
System.out.println("Logitech use keyword to set channel.");
}
}


class DependentRemoteControl implements ITV {
@Override
public void on () {
System.out.println(" Forced to change, if TV interface changes ");
}

@Override
public void off () {
System.out.println(" Forced to change, if TV interface changes ");
}

@Override
public void switchChannel (int channel) {
System.out.println(" Forced to change, if TV interface changes ");
}

}
8 changes: 7 additions & 1 deletion pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
*/
public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
System.out.println("Bridge cook example ");
ITV tv = new SonyTV();
// DependentRemoteControl sub classes TV interface
// and is forced to change even with minor changes in TV interface
DependentRemoteControl remote = new DependentRemoteControl();
remote.on();
remote.switchChannel(23);
}
}
50 changes: 50 additions & 0 deletions pattern/src/com/premaseem/ITV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/

public interface ITV {
public void on ();

public void off ();

public void switchChannel (int channel);
}

class SamsungTV implements ITV {
@Override
public void on () {
System.out.println("Samsung is turned on.");
}

@Override
public void off () {
System.out.println("Samsung is turned off.");
}

@Override
public void switchChannel (int channel) {
System.out.println("Samsung: channel - " + channel);
}
}

class SonyTV implements ITV {

@Override
public void on () {
System.out.println("Sony is turned on.");
}

@Override
public void off () {
System.out.println("Sony is turned off.");
}

@Override
public void switchChannel (int channel) {
System.out.println("Sony: channel - " + channel);
}
}