Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between a bridge and an interface

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?

like image 918
user472875 Avatar asked Dec 29 '22 05:12

user472875


1 Answers

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.

like image 142
Aaron Digulla Avatar answered Jan 05 '23 14:01

Aaron Digulla