It is part of structure design pattern. It is
used to separate the abstraction from the
implementation.
It has two
parts
1.
Abstraction
2.
Implementation
This pattern
allow abstraction and implementer to develop in dependently. So client can accesses
only abstraction class without disturbing implementer class
The Implementation defines the
interface for all implementation classes. It doesn't have to match the
Abstraction's interface. In fact, the two interfaces can be entirely different.
Typically, the Implementation interface provides only primitive operations,
while the Abstraction defines higher-level operations based on those primitives
class.
Elements of Bridge design pattern
are:
·
Abstraction – core of the bridge design pattern and defines the crux. Contains a
reference to the implementer.
·
Refined Abstraction – Extends the abstraction takes the finer detail one level below. Hides
the finer elements from implements.
·
Implementer – It defines the interface for implementation classes. This interface does
not need to correspond directly to the abstraction interface and can be very
different. Abstraction imp provides an implementation in terms of operations
provided by Implementer interface.
·
Concrete Implementation – Implements the above implementer by providing concrete implementation.
Code:
#include<iostream>
class Implementation {
public:
virtual
~Implementation() {}
virtual std::string
OperationImplementation() const = 0;
};
class ConcreteImplementationA : public Implementation {
public:
std::string
OperationImplementation() const override {
return
"ConcreteImplementationA: Here's the result on the platform A.\n";
}
};
class ConcreteImplementationB : public Implementation {
public:
std::string
OperationImplementation() const override {
return
"ConcreteImplementationB: Here's the result on the platform B.\n";
}
};
class Abstraction {
protected:
Implementation*
implementation_;
public:
Abstraction(Implementation* implementation) :
implementation_(implementation) {
}
virtual
~Abstraction() {
}
virtual std::string
Operation() const {
return
"Abstraction: Base operation with:\n" +
this->implementation_->OperationImplementation();
}
};
class ExtendedAbstraction : public Abstraction {
public:
ExtendedAbstraction(Implementation*
implementation) : Abstraction(implementation) {
}
std::string
Operation() const override {
return
"ExtendedAbstraction: Extended operation with:\n" +
this->implementation_->OperationImplementation();
}
};
void ClientCode(const Abstraction& abstraction) {
// ...
std::cout <<
abstraction.Operation();
// ...
}
int main() {
Implementation*
implementation = new ConcreteImplementationA;
Abstraction*
abstraction = new Abstraction(implementation);
ClientCode(*abstraction);
std::cout <<
std::endl;
delete
implementation;
delete abstraction;
implementation = new
ConcreteImplementationB;
abstraction = new
ExtendedAbstraction(implementation);
ClientCode(*abstraction);
delete
implementation;
delete abstraction;
return 0;
}
Result :

