I am trying to read design patterns and currently going thru Bridge Pattern.
It states that
Decouple the functional abstraction from the implementation so that the two can vary independently
I was going thru this example on this link : https://www.journaldev.com/1491/bridge-design-pattern-java
Could somebody explain me how this example to this bold statement?
Thanks a lot.
Bridge
is splitting the interface and implementation in multiple parts. In your example you'll get 2 different interfaces Shape
, Color
. They will generate their own class hierarchies and because they are independent they can both vary.
You'll end up with multiple shapes and multiple colors that can be combined at runtime. This is achieved using composition
instead of inheritance
. Each instance of a Shape
needs an instance of a Color
when it gets created and that's the way you get a red triangle or a green pentagon or any other combination of a Shape
and a Color
.
The hierarchies aren't tightly coupled and they only communicate at an interface level.