开发者

The difference between a bridge and an interface

开发者 https://www.devze.com 2023-01-25 14:32 出处:网络
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

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 forcin开发者_高级运维g 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.


"Interface" generally refers to what is public in a class. As far as I understand it, in the Bridge pattern you actually have separate classes for the interface and the implementation.


An interface is a contract that an implementation must obey. A bridge uses interfaces to achieve its goal which is to separate some concrete implementation from the client interface it agrees with so that the client needs little (or rather no) knowledge of the details of how the service it is using works.


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

There's a big difference, one is a non-universal language specific detail (and for those language which implement an Interface type your statement functions similarly to a Bridge), and the other is universal to all OO languages (that I know of). For the "interface" of a class, then no your statement is not true.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号