The basic idea behind Bridge Pattern is to decouple internally joined hierarchy - to form more like a mix and match object - an object which can consist of different smaller objects - taken from different layers of different inheritance hierarchy.
It’s like the formation of different inheritance hierarchies in designing a model and then picking from here and there to form the ultimate object.
One major advantage of such decoupling is that, it drastically reduces the number of classes to be maintained in the future to completely describe the inheritance hierarchy - it’s like an open-ended designing - which can be further extended in the future where each and every inheritance hierarchy can be expanded independently - without giving much headache for the final outcome. A much cleaner and smarter designing approach.
As the number of classes will be drastically reduced, so will be the final code footprint.
Please have a look at the below mentioned designing approach and see how after applying Bridge Pattern, we have reduced the number of classes from 15 to just 9 in representing a OOAD system.
Case 1:
The model/hierarchy done without bridge pattern:

Total Number of classes:15
Case 2:
Model using bridge pattern:

Total number of classes : 9
Source Code:
//============================================================================ // Name : Bridge.cpp // Author : Ridit // Version : // Copyright : @Ridit... // Description : Hello World in C++, Ansi-style //============================================================================
#include <iostream> #include <memory> #include "FourWheelers.h" #include "Car.h" #include "Truck.h" #include "Gear.h" #include "Colour.h" #include "SmallGear.h" #include "BigGear.h" #include "White.h" #include "Red.h" using namespace std;
int main() { unique_ptr<Car> car = make_unique<Car>();
unique_ptr<Truck> truck = make_unique<Truck>();
unique_ptr<SmallGear> smallGear = make_unique<SmallGear>(); unique_ptr<Red> red = make_unique<Red>();
car->setColour(std::move(red));
car->setGear(std::move(smallGear));
cout<<typeid(car->getGear()).name()<<endl;//see the name mangling
cout<<typeid(car->getColour()).name()<<endl;//see the name mangling return 0; } |
/* * FourWheelers.h * * Created on: 17-Aug-2022 * Author: ridit */
#ifndef FOURWHEELERS_H_ #define FOURWHEELERS_H_ #include <iostream> #include <memory> #include "Gear.h" #include "Colour.h" using namespace std; class FourWheelers { protected: unique_ptr<Gear> gear; unique_ptr<Color> color; public: FourWheelers(){
} virtual ~FourWheelers(){
}
unique_ptr<Color> getColor() { return std::move(color); }
void setColor(unique_ptr<Color> inColor) { color = std::move(inColor); }
unique_ptr<Gear> getGear() { return std::move(gear); }
void setGear(unique_ptr<Gear> inGear) {
gear = std::move(inGear);
} };
#endif /* FOURWHEELERS_H_ */
|
/* * Car.h * * Created on: 17-Aug-2022 * Author: ridit */
#ifndef CAR_H_ #define CAR_H_
#include "FourWheelers.h"
class Car: public FourWheelers { public: Car(){
} virtual ~Car(){
} };
#endif /* CAR_H_ */ |
/* * Truck.h * * Created on: 17-Aug-2022 * Author: ridit */
#ifndef TRUCK_H_ #define TRUCK_H_
#include "FourWheelers.h"
class Truck: public FourWheelers { public: Truck(){
} virtual ~Truck(){
} };
#endif /* TRUCK_H_ */ |
/* * Gear.h * * Created on: 17-Aug-2022 * Author: ridit */
#ifndef GEAR_H_ #define GEAR_H_ #include <iostream> using namespace std;
class Gear { public:
Gear(){
} virtual ~Gear(){
} }; #endif /* GEAR_H_ */ |
/* * Colour.h * * Created on: 17-Aug-2022 * Author: ridit */
#ifndef COLOUR_H_ #define COLOUR_H_ #include <iostream> using namespace std;
class Color{ public:
Color(){
} virtual ~Color(){
} };
#endif /* COLOUR_H_ */ |
/* * BigGear.h * * Created on: 17-Aug-2022 * Author: ridit */
#ifndef BIGGEAR_H_ #define BIGGEAR_H_
#include "Gear.h"
class BigGear: public Gear { public: BigGear(){
} virtual ~BigGear(){
}
};
#endif /* BIGGEAR_H_ */
|
/* * SmallGear.h * * Created on: 17-Aug-2022 * Author: ridit */
#ifndef SMALLGEAR_H_ #define SMALLGEAR_H_
#include "Gear.h"
class SmallGear: public Gear { public: SmallGear(){
} virtual ~SmallGear(){
}
};
#endif /* SMALLGEAR_H_ */ |
Output

Done using raw pointer:
//============================================================================ // Name : Bridge.cpp // Author : Ridit // Version : // Copyright : @Ridit... // Description : Hello World in C++, Ansi-style //============================================================================
#include <iostream> #include "FourWheelers.h" #include "Car.h" #include "Truck.h" #include "Gear.h" #include "Colour.h" #include "SmallGear.h" #include "BigGear.h" #include "White.h" #include "Red.h" using namespace std;
int main() { Car* car = new Car();
Truck* truck = new Truck();
SmallGear* smallGear = new SmallGear(); Red* red = new Red();
car->setColour(red);
car->setGear(smallGear);
cout<<typeid(car->getGear()).name()<<endl;
cout<<typeid(car->getColour()).name()<<endl;
return 0; } |
/* * FourWheelers.h * * Created on: 24-Aug-2022 * Author: ridit */
#ifndef FOURWHEELERS_H_ #define FOURWHEELERS_H_ #include <iostream> #include <memory> #include "Gear.h" #include "Colour.h" using namespace std; class FourWheelers { protected: Gear* gear; Color* color; public: FourWheelers(){
} virtual ~FourWheelers(){
}
Color* getColor() { return color; }
void setColor(Color* inColor) { color = inColor; }
Gear* getGear() { return gear; }
void setGear(Gear* inGear) {
gear = std::move(inGear);
} };
#endif /* FOURWHEELERS_H_ */
|
/* * Car.h * * Created on: 24-Aug-2022 * Author: ridit */
#ifndef CAR_H_ #define CAR_H_
#include "FourWheelers.h"
class Car: public FourWheelers { public: Car(){
} virtual ~Car(){
} };
#endif /* CAR_H_ */ |
/* * Truck.h * * Created on: 24-Aug-2022 * Author: ridit */
#ifndef TRUCK_H_ #define TRUCK_H_
#include "FourWheelers.h"
class Truck: public FourWheelers { public: Truck(){
} virtual ~Truck(){
} };
#endif /* TRUCK_H_ */ |
/* * Gear.h * * Created on: 24-Aug-2022 * Author: ridit */
#ifndef GEAR_H_ #define GEAR_H_ #include <iostream> using namespace std;
class Gear { public:
Gear(){
} virtual ~Gear(){
} }; #endif /* GEAR_H_ */ |
/* * Colour.h * * Created on: 24-Aug-2022 * Author: ridit */
#ifndef COLOUR_H_ #define COLOUR_H_ #include <iostream> using namespace std;
class Color{ public:
Color(){
} virtual ~Color(){
} };
#endif /* COLOUR_H_ */ |
/* * BigGear.h * * Created on: 24-Aug-2022 * Author: ridit */
#ifndef BIGGEAR_H_ #define BIGGEAR_H_
#include "Gear.h"
class BigGear: public Gear { public: BigGear(){
} virtual ~BigGear(){
}
};
#endif /* BIGGEAR_H_ */
|
/* * SmallGear.h * * Created on: 24-Aug-2022 * Author: ridit */
#ifndef SMALLGEAR_H_ #define SMALLGEAR_H_
#include "Gear.h"
class SmallGear: public Gear { public: SmallGear(){
} virtual ~SmallGear(){
}
};
#endif /* SMALLGEAR_H_ */ |
/* * Red.h * * Created on: 24-Aug-2022 * Author: ridit */
#ifndef RED_H_ #define RED_H_
#include "Colour.h"
class Red: public Color { public: Red(){
} virtual ~Red(){
}
};
#endif /* RED_H_ */ |
/* * White.h * * Created on: 24-Aug-2022 * Author: ridit */
#ifndef WHITE_H_ #define WHITE_H_
#include "Colour.h"
class White: public Color { public: White(){
} virtual ~White(){
}
};
#endif /* WHITE_H_ */
|
Output

And here’s the Bridge Pattern implemented in Python.
Python Source Code
from abc import ABC
class Colour(ABC):
def __init__(self):
pass
class ColourRed(Colour):
def __init__(self):
print("this is the colour red")
class ColourWhite(Colour):
def __init__(self):
print("this is the colour white")
class Gear(ABC):
def __init__(self):
pass
class SmallGear(Gear):
def __init__(self):
print("This is small gear")
class BigGear(Gear):
def __init__(self):
print("THis is big gear")
class Vehicle(ABC):
def setGear(self,gear):
self.gear = gear
def setColour(self,colour):
self.colour = colour
def display(self):
print("The colour is ",type(self.colour),"The gear is ",type(self.gear))
class Car(Vehicle):
def __init__(self):
pass
class Truck(Vehicle):
def __init__(self):
pass
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
colour = ColourRed()
gear = SmallGear()
car = Car()
car.setGear(gear)
car.setColour(colour)
car.display()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
Java Source Code:
Abstract Class Gear:
package com.ridit.java.BridgePattern;
public abstract class Gear {
public Gear() {
}
}
Class SmallGear:
package com.ridit.java.BridgePattern;
public class SmallGear extends Gear{
public SmallGear() {
System.out.println("this is small gear");
}
}
Class BigGear:
package com.ridit.java.BridgePattern;
public class BigGear extends Gear{
public BigGear() {
System.out.println("this is big gear");
}
}
Abstract Class Colour:
package com.ridit.java.BridgePattern;
public abstract class Colour {
public Colour() {
}
}
Class Red:
package com.ridit.java.BridgePattern;
public class Red extends Colour {
public Red(){
System.out.println("this is colour red");
}
}
Class White:
package com.ridit.java.BridgePattern;
public class White extends Colour {
public White(){
System.out.println("this is colour white");
}
}
Abstract class Vehicle:
package com.ridit.java.BridgePattern;
public abstract class Vehicle {
private Gear gear;
private Colour colour;
public Gear setGear(Gear gear) {
return this.gear = gear;
}
public Colour setColour(Colour colour) {
return this.colour = colour;
}
public void display() {
System.out.println("the colour is " + this.colour.getClass() + " and the gear is " + this.gear.getClass());
}
}
Class Car:
package com.ridit.java.BridgePattern;
public class Car extends Vehicle {
public Car() {
}
}
Class Truck:
package com.ridit.java.BridgePattern;
public class Truck extends Vehicle {
public Truck() {
}
}
Main:
package com.ridit.java.BridgePattern;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Gear gear = new SmallGear();
Colour colour = new Red();
Vehicle car = new Car();
car.setGear(gear);
car.setColour(colour);
car.display();
}
}
Output:
