Can anybody elaborate the Bridge design pattern and the Decorator pattern for me. I found it similar in some way. I don't know how to distinguish it?
My understanding is that in Bridge, it separate the implementation from the interface, generally you can only apply one implementation. Decorator is kind of wrapper, you can wrap as many as you can.
For example,
Bridge pattern
class Cellphone {
private:
Impl* m_OS; // a cellphone can have different OS
}
Decorator pattern
class Shirt {
private:
Person * m_p; //put a shirt on the person;
}
Strategy Pattern is used for Behavioural decisions, while Bridge Pattern is used for Structural decisions. Brigde Pattern separats the abstract elements from the implementation details, while Strategy Pattern is concerned making algorithms more interchangeable.
A Bridge pattern can only be implemented before the application is designed. Allows an abstraction and implementation to change independently whereas an Adapter pattern makes it possible for incompatible classes to work together.
Decorator Pattern says wrap an original object and add additional features in the wrapper object. So structurally speaking - Wrappers follow decorator pattern. Adapter pattern says changing one object by creating an instance of it and adding functionalities to it.
Decorator:
Refer to sourcemaking article for more details.
UML diagram of Decorator from wikipedia:
Bridge pattern:
Use the Bridge pattern when:
UML diagram of Bridge from wikipedia:
From UML diagram, you can observe the difference:
In Decorator pattern, Decorator is implementing Component, which will be replaced by ConcreteComponent at run time.
In Bridge pattern, RedefinedAbstraction is not implementing Implementor. Instead, it uses composition so that Implementor can vary dynamically at run time without client knowledge.
Decorator can't decouple abstraction from implementation unlike Bridge pattern.
Few more useful posts:
When to Use the Decorator Pattern?
When do you use the Bridge Pattern? How is it different from Adapter pattern?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With