Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bridge pattern vs. decorator pattern

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;

}
like image 307
skydoor Avatar asked Feb 12 '10 19:02

skydoor


People also ask

What is difference between bridge and strategy pattern?

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.

What is the difference between Bridge pattern and Adapter pattern?

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.

What is the difference between a Decorator and Adapter design patterns?

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.


1 Answers

Decorator:

  1. Add behaviour to object at run time. Inheritance is the key to achieve this functionality, which is both advantage and disadvantage of this pattern.
  2. It enhances the behaviour of interface.
  3. Decorator can be viewed as a degenerate Composite with only one component. However, a Decorator adds additional responsibilities - it isn't intended for object aggregation.
  4. Decorator supports recursive composition
  5. The Decorator class declares a composition relationship to the LCD (Lowest Class Denominator) interface, and this data member is initialized in its constructor.
  6. Decorator is designed to let you add responsibilities to objects without sub-classing

Refer to sourcemaking article for more details.

UML diagram of Decorator from wikipedia:

enter image description here

Bridge pattern:

  1. Bridge is structural pattern
  2. Abstraction and implementation are not bound at compile time
  3. Abstraction and implementation - both can vary without impact in client

Use the Bridge pattern when:

  1. you want run-time binding of the implementation,
  2. you have a proliferation of classes resulting from a coupled interface and numerous implementations,
  3. you want to share an implementation among multiple objects, you need to map orthogonal class hierarchies.

UML diagram of Bridge from wikipedia:

enter image description here

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?

like image 93
Ravindra babu Avatar answered Sep 29 '22 23:09

Ravindra babu