Alex Rivera | Logout

bridge pattern vs. decorator pattern

Asked 2010-02-12T19:28:29.000
14

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;

}
Edit
Report

1 Answer

1

Brian is correct. I'll add that conceptually, the client will "know" it's using a bridge to an underlying object, but with a decorator, the client will be unable to know there's a decorator layer between it and the target object.

The purpose of the bridge is to create a layer of abstraction to protect the client. The purpose of the decorator is to add functionality to the object without the client knowing. Most decorators will pass along all function calls directly to a pointer to their parent class, except for functions relating directly to what the decorator is designed to change.

answered 2010-02-12T19:40:57.083

Your Answer