Bridge design pattern in java

As stated by Gof:
“Decouple an abstraction from its implementation so that the two can vary independently”.When there are inheritance hierarchies in both interface and implementation then you loose coupling because of interdependence.In other words,Decoupling interface from implementation and hiding implementation detail of abstraction from client is main objectives of bridge design pattern.

Also known as:

Handle/Body

UML diagram of generic bridge design pattern:

Elements:

  • Abstraction
    • defines the abstraction’ s interface.
    • maintain reference to object of type implementor.
  • RefinedAbstraction
    • extends the interface defined by abstraction.
  • Implementor
    • defines the interface for implementation classes.This interface doesn’t have to correspond exactly to abstraction’s interface;in fact the two interfaces can be quite different.Typically the implementor interface provides only primitive operations,and abstraction defines higher-level operations based on these primitives.
  • ConcreteImplementor
    • implements the implementor interface and defines its concrete implementation.

When to use it:

  • Abstraction and implementation should be bound at compile time.
  • Both abstraction and implementation can have different hierarchies independently.You want to extend both hierarchies by subclassing.
  • Changes in implementation should have no impact on abstraction.
  • If you want to hide implementation of abstraction from client.
  • Best use when you have multiple implementation.

WorkFlow:

Abstaction forwards client request to its implementor object.

Example:

Before Applying bridge design pattern:
After applying bridge design pattern:
 

UML diagram for example:

Comparing to above generic description of bridge pattern.

My example includes following elements:
  • Shape(Abstraction)
  • Rectangle(RefinedAbstraction)
  • Circle(RefinedAbstraction)
  • Color(Implementor)
  • RedColor(ConcreteImplementor)
  • BlueColor(ConcreteImplementor)

 Java code for all above classes:

We have two inheritance hierarchies here.i.e Shape and Color.Now with help of bridge design pattern both can vary independently.
This is our abstraction class.
 Shape.java(Abstraction):

Rectangle.java(RefinedAbstraction):

Circle.java(RefinedAbstraction):


This is our implementor class.
Color.java(Implementor):

RedColor.java(ConcreteImplementor):

BlueColor.java(ConcreteImplementor):

BridgeDesignPatternMain.java:

 Output:

Related patterns:

An Abstract Factory can create and configure a particular bridge.

The Adapter design pattern convert interface of a class ito another interface clients expects.It is usually applied to system after they are designed.Bridge on other hand is used up-front in a design to let abstractions and implementation vary independently.

Related Posts

  • Detect keypress in Python
    12 June

    Detect keypress in Python

    Table of ContentsUsing the keyboard module to detect keypress in PythonUsing the pynput module to detect keypress in PythonUsing the msvcrt module to detect keypress in Python Python allows us to work with user input in its programs. We can also work with hardware devices in Python. In this article, we will discuss how to […]

  • Write a list to a file in Python
    12 June

    Write a list to a file in python

    Table of ContentsUsing the write() function to write a list to a file in PythonUsing the writelines() function to write a list to a file in PythonUsing the pickle.dump() function to write a list to a file in PythonUsing the json.dump() function to write a list to a file in PythonUsing the unpack operator * […]

  • Copy a dictionary in Python
    12 June

    7 ways to copy a dictionary in python

    Table of ContentsUsing the copy() function to copy a dictionary in PythonUsing the deepcopy() function to copy a dictionary in PythonUsing the dict() function to copy a dictionary in PythonUsing the unpack operator ** to copy a dictionary in PythonUsing the update() function to copy a dictionary in PythonUsing the dictionary comprehension method to copy […]

  • Escape quotes in Python
    11 June

    Escape quotes in Python

    Table of ContentsUsing \ to escape quotes in PythonUsing \ with replace() to escape quotes in PythonUsing json.dumps to escape quotes in PythonUsing triple single or double quotes to escape quotes in pythonUse r to include \ and quotes in String 💡 Outline You can use \ to escape quotes in Python. If you want […]

  • Merge two vectors in C++
    11 June

    Merge two Vectors in C++

    Table of ContentsUsing merge() to merge two vectors in C++Using set_union() to merge two vectors in C++Using insert() to combine two vectors in C++Using move() & back_inserter to merge two vectors in C++Using copy() and back_inserter to concatenate two vectors in C++Using make_move_iterator() and insert() to merge two vectors in C++Conclusion vectors are like dynamic […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.