As far as I understand, the purpose of a Bridge pattern is, quoting from Wikipedia, "to decouple an abstraction from its implementation". Well isn't that exactly what an interface does. By deciding on an interface and forcing a class to use that interface, any other class can interact with this one without the need for any knowledge about the internal workings.
So is an interface equivalent to a Bridge?
Interface just means "public API" of something: That is the contract against which you write software. Java uses the keyword interface
to define classes without code that contain such contracts.
The bridge pattern is a design pattern. It stands for decoupling. You could say Java interfaces are one way to implement this pattern.
Note that bridges usually expose the full API while Java interfaces can expose only part of the API. Example: You have a class Foo
with two methods: bar()
and baz()
.
A bridge is anything which has the same public API as Foo
and which can be used in any place where Foo
can be used.
With interfaces, you can have two. One contains bar()
and the other baz()
. Any class which implements both is a valid implementation for Foo
but you can also have classes which implement only one of them.
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