top of page
Search
Writer's pictureAbhinaw Tripathi

Design Pattern - Bridge Design Pattern


What is Bridge Design Pattern?

Bridge DP is Structural Pattern.Bridge pattern is used to separate out the interface from its implementation.

For Example: Let us take case of Electrical equipments we have at home and their switches.Switch of the fan.the switch is the interface and the actual implementation is the running of the fan once its switched-on .both the switch and fan are independent of each other.

Implementation:

/**

* @author Abhinaw.Tripathi

*

*/

interface Tv

{

public void powerOn();

public void powerOff();

public void channelChange(int channel);

}

class Google implements Tv

{

@Override

public void powerOn() {

// TODO Auto-generated method stub

//google specific code

}

@Override

public void powerOff() {

// TODO Auto-generated method stub

//google specific code

}

@Override

public void channelChange(int channel) {

// TODO Auto-generated method stub

//google specific code

}

}

class Samsung implements Tv

{

@Override

public void powerOn() {

// TODO Auto-generated method stub

//Samsung specific code

}

@Override

public void powerOff() {

// TODO Auto-generated method stub

//Samsung specific code

}

@Override

public void channelChange(int channel) {

// TODO Auto-generated method stub

//Samsung specific code

}

}

abstract class TVremoteControl

{

private Tv implementor;

public void powerOn()

{

implementor.powerOn();

}

public void powerOff()

{

implementor.powerOff();

}

public void setChannel(int channnel)

{

implementor.channelChange(channnel);

}

}

class ConcreteTVRemoteControl extends TVremoteControl

{

private int currentChannel;

public void nextChannel()

{

currentChannel++;

setChannel(currentChannel);

}

public void prevChannel()

{

currentChannel --;

setChannel(currentChannel);

}

}

public class BridgePatternTest {

/**

*

*/

public BridgePatternTest() {

// TODO Auto-generated constructor stub

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ConcreteTVRemoteControl remote=new ConcreteTVRemoteControl();

remote.nextChannel();

remote.setChannel(12);

remote.prevChannel();

remote.powerOff();

remote.powerOn();

remote.powerOff();

}

}

When to Use Bridge Design Pattern:

1) This pattern should be used when both the class as well as what it does very often.

2)It can also be thought of as two layer of abstraction.

3)It is useful in times when you need to switch between multiple implementations at runtime.

2 views0 comments

Recent Posts

See All
bottom of page