TheWommies
TheWommies

Reputation: 5072

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?

Upvotes: 3

Views: 1862

Answers (1)

Andrew
Andrew

Reputation: 362

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.

Upvotes: 11

Related Questions