3
votes

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 enter image description here

Could somebody explain me how this example to this bold statement?

Thanks a lot.

1
Note to anyone attempting an answer: It might help to also view the original class diagram, before applying to bridge pattern, in the same link above.Tim Biegeleisen
@Tim It will be great if you can attempt an answer explaining. ThanksNicky
Sounds like bridge is a way to implement multiple inheritance through composition?Juan Mendes

1 Answers

3
votes

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.