Bridge design Pattern

The bridge pattern allows the Abstraction and the Implementation to be developed independently and the client code can access only the Abstraction part without being concerned about the Implementation part.

Decouple an abstraction from its implementation so that the two can vary independently

Need for Bridge Design Pattern

When there are inheritance hierarchies creating concrete implementation, you loose flexibility because of interdependence.

Decouple implentation from interface and hiding implementation details from client is the essense of bridge design pattern.

When to use the Bridge Pattern

  • When there is a need to void the permanent binding or tight coupling between an abstraction and implementation. In this case, implementation may select or switch at run-time
  • When there is a need for extending the abstraction and the composite implementation separately by subclasses.
  • When the change in extended implementations should not impact the client
  • When there is a hierarchical propagation of several categories of classes in the system

Advantageous of Bridge Design Pattern

  • Bridge patterns allows independent variation between two abstract and implementor systems
  • It avoids the client code binding to a certain implementation
  • Abstraction and implementation can be clearly separated allowing easy future extension
  • Provides a good solution for cross-platform development
  • Well suited for testing with stubs in initial development cycle


Complete code for kotlin bridge pattern


package structural
interface Switch {
    var appliance: Appliance
    fun turnOn()
}
interface Appliance {
    fun run()
}
class RemoteControl(override var appliance: Appliance) : Switch {
    override fun turnOn() = appliance.run()
}
class TV : Appliance {
    override fun run() = println("TV turned on")
}
class VacuumCleaner : Appliance {
    override fun run() = println("VacuumCleaner turned on")
}
fun main(args: Array<String>) {
    var tvRemoteControl = RemoteControl(appliance = TV())
    tvRemoteControl.turnOn()
    var fancyVacuumCleanerRemoteControl = RemoteControl(appliance = VacuumCleaner())
    fancyVacuumCleanerRemoteControl.turnOn()
}			

About Author

This article is written by Pavan, who is serving notice period in an MNC, Bangalore. He thought, let the articles speak rather than his image. He is also the same person who created the reporter for Protractor Jasmine

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions