简体   繁体   English

GoF设计模式网桥/适配器/装饰器

[英]GoF Design patterns Bridge/Adapter/Decorator

I am reading up on design patterns, and there is a question I don't feel I can answer myself. 我正在阅读设计模式,有一个我觉得自己无法回答的问题。 Are Adapter, Bridge and Decorator structurally different, or are they coded the same but just applied with different semantics? Adapter,Bridge和Decorator在结构上是否不同,或者它们的编码是否相同,但只是应用了不同的语义?

Structurally, the patterns are very similar. 在结构上,模式非常相似。 A lot of the difference is in the intent of the pattern and not the way they are built. 模式的意图有很多不同,而不是模式的构建方式。

The adapter pattern is used to adapt a class with one interface to that of another interface. 适配器模式用于使具有一个接口的类适应另一个接口的类。 Typically, the class you are adapting is legacy code or in a library that you can't access (or at least can't change). 通常,您要改编的类是遗留代码或您无法访问(或至少不能更改)的库中。 One key difference with the adapter is that the interface that you are adapting and what you are providing back are different . 与适配器的主要区别在于,您要适配的接口和您提供的接口是不同的

The bridge pattern is very similar to adapter, in that it can bridge a class with one interface to that of another interface. 桥接模式与适配器非常相似,因为它可以将具有一个接口的类桥接到另一个接口的类。 However, the key difference is intent . 但是,关键区别在于意图 With the bridge pattern, the class was explicitly designed to work this way. 通过桥接模式,该类被明确设计为以这种方式工作。 The developer will still have access to the class that is being delegated to, but the developer intentionally chose to design it this way. 开发人员仍然可以访问委派给该类的类,但是开发人员有意选择以这种方式进行设计。

The decorator pattern will return the same interface of the class that is being decorated. 装饰器模式将返回正在装饰的类的相同接口。 It is used to extend the behavior of an existing class, not to change its interface. 它用于扩展现有类的行为 ,而不更改其接口。

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

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