The difference between bridge and interface

As I understand it, the purpose of the Bridge template is to quote from Wikipedia "to separate the abstraction from its implementation." Well, that’s not what the interface does. By deciding on an interface and forcing a class to use this interface, any other class can interact with it without the need for any knowledge of internal work.

So is the interface equivalent to a bridge?

+4
source share
4 answers

An interface simply means a “public API” of something: it is a contract against which you write software. Java uses the interface keyword to define classes without code containing such contracts.

The bridge pattern is a design pattern. It means a denouement. You could say that 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 Foo class with two methods: bar() and baz() .

A bridge is all that has the same open API as Foo , and which can be used anywhere Foo can be used.

With interfaces, you can have two. One contains bar() , and the other baz() . Any class that implements both versions is a valid version for Foo , but you can also have classes that implement only one of them.

+8
source

An “interface” usually refers to what is publicly available in a class. As far as I understand, in the Bridge template you actually have separate classes for the interface and implementation.

+1
source

An interface is a contract that an implementation must obey. The bridge uses interfaces to achieve its goal, which is to separate the specific implementation from the client interface with which it agrees, so that the client does not need a little (or rather, no) knowledge of the details of how the service that it uses .

0
source

Are you talking about Interface (as in the language construct present in many Java-like languages)? Or just an “interface”, as in the “things that a type exposes to its clients”?

There is a big difference, one is not a universal language-specific detail (and for those languages ​​that implement the Interface type, your operator functions similar to a bridge), and the other is universal for all OO languages ​​(what I know about). For the "interface" of the class, then your statement is incorrect.

0
source

All Articles