Bridge Pattern

Type : Structural Design Pattern

Summary

Decouple an abstraction from its implementation so that the two can vary independently. Abstraction and Implementation can have own hierarchies, which makes it easy to change the Implementation Classes without impacting the Client.

Details

It can be considered as double abstraction. Bridge pattern separates the implementation from abstraction in object oriented world.
As a result of Bridge pattern two different object hierarchies can be assumed. The abstraction hierarchies and implementations hierarchies. The implementation hierarchies can take the complexity of implementations that very and not related to the other hierarchies.

Bridge Pattern utilizes Composition and Inheritance. It's purpose is to make the Code more manageable and easy to refactor.

Advantages:


Bridge pattern bridges two hierarchies, it results in less number of Classes, since the implementations can be plugged in to the abstraction in run time.

Bride in a way favors Composition over pure inheritance to make the dynamic pluggable implementation in runtime.


Example:


For our case, we will consider a simple example of Home theater system. Home theater can use different technologies to read or get the content. Like it can have support for DVD, Blue Ray or even can get online content.  Home theater can also have other features in terms of its sound capability.

For our case we will consider SimpleEntertainmentSystem and DolbyEntertainmentSystem in terms of sound capabilities. Also we have different kind of MediaPlayer (CloudPlayer, HDMediaPlayer, BlueRayPlayer) etc.



EntertainmentSystem theater abstract the capabilities of a Home theater (play, pause, maxSound)
EntertainmentSystem has-a relationship to the MediaPlayer
MediaPlayer represents the implementation hierarchy

Class Diagram






Advantages

 It is like a plugin architecture where we can plugin the implementation to the abstraction
 It reduces the number of classes since we can plug the implementation to a class to have desired   behaviour.
 With this approach it is easy to share the Classes between modules. 
 

Comments

Popular posts from this blog

Difference between volatile and synchronized

Converting Java Map to String

Masking Credit Card number in Java