161

I've been reading both definitions and they seem quite the same.

Could anyone point out what are their differences?

John Smith
  • 7,243
  • 6
  • 49
  • 61
devoured elysium
  • 101,373
  • 131
  • 340
  • 557

16 Answers16

190

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.

awshepard
  • 2,627
  • 1
  • 19
  • 24
  • 4
    Great analogy using remote control. Adapter pattern explanation is OK, but it would be great to come up with the same kind of analogy. – Kevin Le - Khnle Jun 02 '10 at 20:37
  • 2
    Excellent analogy indeed! A real world Java example of the Adapter pattern may help understanding it better: [`InputStreamReader`](http://java.sun.com/javase/6/docs/api/java/io/InputStreamReader.html) which adapts `InputStream` to `Reader` and [`OutputStreamWriter`](http://java.sun.com/javase/6/docs/api/java/io/OutputStreamWriter.html) which adapts `OutputStream` to `Writer` Both which are different abstract types. – BalusC Jun 02 '10 at 20:39
  • 1
    @Khnle - Adapter pattern in action: http://upload.wikimedia.org/wikipedia/commons/8/80/USB_PS2_Adapters.JPG – Eric Petroelje Jun 02 '10 at 20:44
  • @Khnle - added in an Adapter analogy (based on personal experience). @Eric - thanks for the inspiration and great pic! @BalusC - good call on the real world example. – awshepard Jun 02 '10 at 22:10
  • @BalusC - I like your example too. Luckily I know Java. @Eric Petroelje - A picture is worth thousand words :-) @awshepard - if I didn't programming, I now can follow you explanation too :-) – Kevin Le - Khnle Jun 02 '10 at 23:27
  • I know this is a old answer. But i feel that the TV remote analogy better explains a command pattern. Facade according to wikipedia is "Provides a simplified interface".So maybe if the TV remote is just electronic terminals instead if buttons where we have to pass 5 Volts manually to turn them on and we create a button which does that that would be a better analogy IMO. Since is simplifies the remotes from passing 5V to pushing a button – thebenman Mar 16 '18 at 11:09
  • @KevinLe-Khnle keeping on the same remote control analogy, the Adapter is implemented when you program your remote to connect to the specific TV, cable box, etc. The façade is in the button, the Adapter in the IR emitter that sends every device a proper signal. – Carlos Mora Aug 31 '18 at 05:49
  • @thebenman I see your point. but for the example you should consider the fact you are pushing a single button instead of pushing a button in every single remote control device. Staying in the same example, i think a Command can be found in the IR signal itself as the invoke and the IR receiver executing the command changing the device's status. – Carlos Mora Aug 31 '18 at 05:55
180

Adapter == making a square peg fit into a round hole.

Facade == a single control panel to run all the internal components.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
40

The purpose of a

facade is simplicity

adapter is interoperability.

Val Kornea
  • 4,469
  • 3
  • 40
  • 41
25

Facade:

Provides a single interface rather than multiple interfaces that does the similar kind of jobs

Example : API Gateway

Adapter:

A structural pattern to work with incompatible interfaces. It allows the interface of an existing class to be used as another interface

Example : MouseAdapter

Key differences:

Facade defines a new interface, whereas Adapter uses an old interface. Adapter makes two existing interfaces work together

Facade produce a simpler interface Adapter changes an existing interface

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • Love to see diagrams! Just some clarification though, when I change the number/type of parameters does it mean its not an Adapter anymore? like `someMethod(int year, int month)` was delegated to `someMethod(DateTime start, DateTime end)` or lets say `someMethod()` delegated to `someMethod(T param)` – Jaime Sangcap Sep 08 '16 at 07:48
  • If both methods are in same class, it's called overloading. If they are in different classes, it can be adpater if adapter and adaptee relationship is implemented – Ravindra babu Sep 09 '16 at 14:03
  • Really nice explanation. – Duc Filan Sep 14 '17 at 13:35
  • Isn't Facade breaks Single Responsibility Principle? – Himalaya Garg May 29 '22 at 15:11
23

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.

J. Burch
  • 233
  • 1
  • 4
20

Facade is usually contrasted with Adapter.

Facade Adapter
Simplifies multiple complex components with single interface Provides differnet interface for an interface
Works with multiple components Works with single component
Control panel is an example A power adapter is an example
High-level interface Low-level interface
Premraj
  • 72,055
  • 26
  • 237
  • 180
19

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.

Mike Burton
  • 3,010
  • 24
  • 33
8

The difference between these two patterns is clear, but not in the realm of Design Patterns, but Domain Modeling. In the following, I'll explain why.

First, I want to reiterate others have said here, and then I'll add the note:

A Facade is an interface to a subsystem (an external or a legacy system) that simplifies the access for the client (us). Facade hides the interface of the other subsystem (aggregate some calls, or hide some APIs that we don't need), thus your client only accesses that subsystem through this Facade.

On the other hand, an Adapter is a wrapper around another service or object. It makes the wrapped object conform to a standard interface that the client expects. Let's say there is a method on the "Ledger" object, which you need to make a tweak (change its parameters, change its name, etc.). You can wrap it with an adapter.

Now, still the difference might not be clear. That's where I want to bring up the key difference between these two patterns leaving no room for further confusion:

Facade doesn't changes the domain model of the other subsystem, while Adapter does. This is the key difference. Period.

That's why you combine these two when you create an Anticorruption Layer. Let's say you have subsystem which you want to use, but you don't want its domain model to muddle your domain model. What would you do? You'd create an Anticorruption Layer. How? You first create a Facade, that simplifies accessing the interface for the subsystem, and then adapters for the domain objects used in that interface (remember the facade still holds the domain model for the other subsystem), so it conforms to your model.

Many design patterns can be used in domain modeling. This is true for Facade and Adapter design patterns, as well. Although the difference between these two patterns might not be clear in "design pattern" realm, it's more clear in "domain modeling" realm.

Arian
  • 7,397
  • 21
  • 89
  • 177
6

As usual, there exist similarities between several patterns. But I would see it like this:

  • A facade is used to encapsulate an entire layer, and offer some methods to access it "conveniently"
  • An adapter is used, where you have two components that should already work together, but don't, only because of some "unimportant" differences in the interface.
Chris Lercher
  • 37,264
  • 20
  • 99
  • 131
  • 2
    Good explanation. This is the first time I came across the word "unimportant differences" when describing the adapter, which is infact true – Sudara Jan 22 '16 at 08:30
4

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.

Pin
  • 3,746
  • 4
  • 26
  • 33
3

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.

wyldebill
  • 43
  • 2
3

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.

Michael Finger
  • 1,110
  • 9
  • 9
3

Adapter pattern links two incompatible interfaces by providing a new interface.

Facade pattern simplifies a complex subsystem(having multiple components) with a single interface.

Avik Chowdhury
  • 171
  • 1
  • 1
  • 7
2

Facade

Abstracts complexity to provide a simpler interface. Say for example, an computer OS abstracts the complexity of underlying hardware. Or a high-level programing languages(Python/JavaScript) abstracts complexity when compared to a low-level language(C).

Adapter

It's analogues to a hardware adapters. Say you want to connect a USB device to a serial port, you will need a USB-serial port adapter.

Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38
2

The main goal of the Facade pattern is to make the class or subsystem easier to use, while the main goal of the Adapter pattern is to adjust the interface to what the client expects.

Ali Bayat
  • 3,561
  • 2
  • 42
  • 43
0

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.

mexique1
  • 1,682
  • 11
  • 18