I've been reading both definitions and they seem quite the same. Could anyone point out what are their differences?
Thanks
I've been reading both definitions and they seem quite the same. Could anyone point out what are their differences?
Thanks
A facade is designed to organize multiple services behind a single service gateway. An adapter is designed to provide a way to use a known interface to access an unknown one.
The Facade Pattern wiki page has a brief note about this.
"An Adapter is used when the wrapper must respect a particular interface and must support a polymorphic behavior. On the other hand, a facade is used when one wants an easier or simpler interface to work with."
I heard an analogy that you should think of your universal remote control that you've set up to work with all your different stereo systems - you press "on" and it turns on your cable box, your receiver, and your TV. Maybe it's a really fancy home theater and it dims the lights and draws the shades too. That's a Facade - one button/function that takes care of a more complicated set of steps.
The Adapter pattern just links two incompatible interfaces.
EDIT: A quick analogy for the Adapter pattern (based on the comments) might be something like a DVI-to-VGA adapter. Modern video cards are often DVI, but you've got an old VGA monitor. With an adapter that plugs into your video card's expected DVI input, and has its own VGA input, you'll be able to get your old monitor working with your new video card.
Adapter pattern allows two,previously incompatible, interfaces to work with each other. Has 2 separate interfaces in play.
The Facade pattern takes a known interface, that is low level/fine grained, and wraps it with a higher level/course grained interface. Has a single interface, that has been simplified by wrapping with another.
Adapter makes two interfaces work together.
Facade exposes a single class to a higher, and more limited level. For example, a view model facade may only expose certain read only properties of a lower level class.
As usual, there exist similarities between several patterns. But I would see it like this:
Adapter == making a square peg fit into a round hole.
Facade == a single control panel to run all the internal components.
Honestly, many patterns could be implemented the same way programmatically -- the difference is in intent.
The Adapter design pattern is meant to 'translate' the interface of one or more classes into an interface that the client expects to use -- the adapter would translate the calls to the expected interface into the actual interface the wrapped classes use.
The Facade pattern is used when a simpler interface is wanted (and again, could be implemented the same way by wrapping the offending classes.) You wouldn't say you're using a facade when the existing interface is incompatible, just when you need to make it more readable, less poorly-designed, etc.
I'll try to explain this in plain words, without much formality.
Imagine you've got some domain classes and from the UI you want to interact with them. A facade can be used to provide functions that can be called from the UI layer so that the UI layer doesn't know about any domain classes other than the facade. That means instead of calling the functions in the domain classes you call a single function from the facade, which will be responsible of calling the needed functions from the other classes.
An adapter, on the other hand, can be used to integrate other external components that might have the same functionality you need but their functions are not called quite the same way. Say you've got a Car
class in your domain and you work with an external car provider that has a Car class defined as well. In this class, you've got the function car.getDoors()
but the external provider has the equivalent car.getNumDoors()
. You don't want to change the way you call this function, so you can use an adapter class to wrap the external Car class so that a call to getDoors()
of the adapter is delegated to getNumDoors()
of the external class.
I've been reading both definitions and they seem quite the same.
Really ?
I have noticed that the term Adapter is sometimes used to describe what is in fact a Stategy, maybe because the word is more expressive.
For example, in Zend Framework, all the Adapter classes are in fact implementations of the Strategy pattern, because they only wrap native code behind classes, to have several behaviours.
Adapters are often used to wrap legacy or "old-style" code.
All these answers are trying to explain a difference that is given for granted. Maybe the point is that a certain abstraction level, both cases are just wrapping an interface with a new one. From this point of view, it would be very helpful to explain why the distinction makes sense, i.e. what is the concept / value / consequence that it means and not just simply saying "in this case, use this, in this other, use that" ... and then discover that at the end (as said in this same discussion) both "this" and "that" are the same ... thing.