I learned Bridge pattern from different articles and I have implemented that as per my understanding . One thing that is confusing me is bridge pattern says
BridgePattern decouples an abstraction from its implementation so that the two can vary independently
what is meaning of this statement? Is implementation resides at in separate jar ?
what is meaning of vary independently statement ?
considering the provided journaldev article, elaborate the answer.
Any help is greatly appreciated.
The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently", introduced by the Gang of Four.
An implementation is not bound permanently to an interface. The implementation of an abstraction can be configured at run-time. It's even possible for an object to change its implementation at run-time. Decoupling Abstraction and Implementor also eliminates compile-time dependencies on the implementation.
Bridge Pattern: The sense of GoF suggested Bridge pattern is decouple the implementation of an component from it's abstraction.
BridgePattern decouples an abstraction from its implementation.
Abstraction and Implementation can vary independently since the concrete class does not directly implement Abstraction ( interface)
Key note: Two orthogonal class hierarchies (The Abstraction hierarchy and Implementation hierarchy) are linked using composition (and not inheritance).This composition helps both hierarchies to vary independently.
Implementation never refers Abstraction. Abstraction contains Implementation interface as a member (through composition).
Coming back to your question regarding the example code in journaldev article :
Shape is Abstraction
Triangle is RedefinedAbstraction
Color is Implementor
RedColor is ConcreteImplementor
A concrete Shape object : Triangle extends Shape but does not implement the Color interface.
public class Triangle extends Shape{
}
RedColor and GreenColor actually implement the Color interface.
The Concrete Shape object (Triangle) is independent of implementing abstraction (i.e. Color interface).
Shape tri = new Triangle(new RedColor());
Here Triangle contains a concrete Color object ( Composition). If there is a change in the Color abstraction (interface), RedColor and GreenColor are responsible for implementing the abstraction of Color interface.
Shapes like Triangle is not affected by changes in contract to the Color interface. So the Color interface can vary independently. This is possible because Shape holds the contract that uses Composition rather than implementation.
In Summary,
Use the Bridge pattern when:
Useful links:
tutorialspoint artice
dzone article
oodesign article
sourcemaking article
Related post:
When do you use the Bridge Pattern? How is it different from Adapter pattern?
This statement simply means, that you can switch implementor, to which abstraction points to, in run-time and everything should work (like in Strategy Pattern; but in Strategy Pattern, only strategies are abstract). It can be also understood as separating two classes, so that they don't have to know about each other more than just their interfaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With