views:

194

answers:

3

Hi,

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;

}
+6  A: 

The Decorator should match the interface of the object you're decorating. i.e. it has the same methods, and permits interception of the arguments on the way in, and of the result on the way out. You can use this to provide additional behaviour to the decorated object whilst maintaining the same interface/contract. Note that the interface of the Decorator can provide additional functionality to create a more useful object.

The Bridge has no such constraint. The client-facing interface can be different from the underlying component providing the implementation, and thus it bridges between the client's interface, and the actual implementation (which may not be client-friendly, subject to change etc.)

Brian Agnew
Decorator may also add convenience methods to the interface that it is decorating.
fuzzy lollipop
Yes. That's a very good point. I'd forgotten that
Brian Agnew
+3  A: 

Your decorator pattern implementation isn't quite right - It would make more sense if you did:

class PersonWearingShirt : IPerson
{
private:
    IPerson * m_p;           //put a shirt on the person;

}

The idea is that, when you decorate a class, you expose the exact same interface. This makes your "decorated" instance look and act like the original. This allows you to wrap an instance many times with multiple decorators, but treat it exactly the same as you treat the original instance.

Reed Copsey
right, I realized that after I posted.
skydoor
How does this differ from the Proxy design pattern?
Thomas Matthews
Decorator is intended to add functionality, "wrapping" an existing object. Multiple decorators can be assigned around a single object (ie: each wraps the previous). Proxy basically acts as an intermediary, adapting it for some reason - often either to handle some functionlity (resource allocation), delay load, etc - but typically, you have one proxy to indirectly provide access to one instance.
Reed Copsey
+1  A: 

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.

JonM