简体   繁体   English

设计模式-适配器和桥接器之间的区别?

[英]Design patterns - Difference between Adapter and Bridge?

I'm reading GoF's Design Patterns and I'm stuck at the Bridge pattern. 我正在阅读GoF的设计模式,但仍停留在Bridge模式。 I can't help but notice it's very similar to the Adapter pattern and I've tried understanding the difference between the two by reading the question on Stack Overflow, but I'm still very confused. 我忍不住注意到它与Adapter模式非常相似,我已经尝试通过阅读Stack Overflow上的问题来尝试理解两者之间的区别,但是我仍然很困惑。

The chapter about the Adapter pattern says that there are two ways to implement it. 关于适配器模式的一章说,有两种方法可以实现它。 First, class adapter and second, object adapter. 首先是类适配器,其次是对象适配器。 One of the benefits of the object adapter (as written in the book) is that you can adapt not only one particular class, but the children classes as well. 对象适配器(如书中所述)的好处之一是,您不仅可以适应一个特定的类,而且还可以适应其子类。

Correct me if I'm wrong, but isn't that kind of Adapter implementation pretty much what Bridge actually is? 如果我错了,请指正我,但是那种适配器实现不是桥实际上是什么吗? In other words, isn't Bridge just the object-implemented version of Adapter pattern where you can use the children classes as well? 换句话说,Bridge不只是您可以在其中使用子类的Adapter模式的对象实现版本吗?

Thanks. 谢谢。

ps If you're going to post a code example, please post it in C++. ps如果要发布代码示例,请以C ++形式发布。

The Bridge wraps the object it represents, but can choose to change that object during runtime for any other object with the same Interface, changing the entire behavior of the Bridge object if needed. Bridge包装了它表示的对象,但是可以选择在运行时为具有相同接口的任何其他对象更改该对象,并在需要时更改Bridge对象的整个行为。 The Adapter is usually created for one specific Class and one specific object. 通常为一个特定的类和一个特定的对象创建适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM