What is the difference between the Facade and Adapter Pattern?

Design PatternsLanguage AgnosticAdapterFacade

Design Patterns Problem Overview


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

Could anyone point out what are their differences?

Design Patterns Solutions


Solution 1 - Design Patterns

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.

Solution 2 - Design Patterns

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

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

Solution 3 - Design Patterns

Facade:

Key takeaways : ( from journaldev article by Pankaj Kumar)

  1. Facade pattern is more like a helper for client applications
  2. Facade pattern can be applied at any point of development, usually when the number of interfaces grow and system gets complex.
  3. Subsystem interfaces are not aware of Facade and they shouldn’t have any reference of the Facade interface
  4. Facade pattern should be applied for similar kind of interfaces, its purpose is to provide a single interface rather than multiple interfaces that does the similar kind of jobs

Facade class diagram:

enter image description here

Adapter:

  1. It is a structural pattern
  2. It is useful to work with two incompatible interfaces
  3. It makes things work after they're designed

Class diagram of Adapter:

enter image description here

You can find more details about Adapter in this SE post:

https://stackoverflow.com/questions/1425171/difference-between-bridge-pattern-and-adapter-pattern/35320081#35320081

Key differences:

  1. Facade defines a new interface, whereas Adapter uses an old interface. Adapter makes two existing interfaces work together as opposed to defining an entirely new one
  2. Adapter and Facade are both wrappers; but they are different kinds of wrappers. The intent of Facade is to produce a simpler interface, and the intent of Adapter is to design to an existing interface

Have a look at sourcemaking article too for better understanding.

Solution 4 - Design Patterns

The purpose of a

facade is simplicity

adapter is interoperability.

Solution 5 - Design Patterns

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.

Solution 6 - Design Patterns

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.

Solution 7 - Design Patterns

>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

Solution 8 - Design Patterns

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.

Solution 9 - Design Patterns

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.

Solution 10 - Design Patterns

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.

Solution 11 - Design Patterns

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.

Solution 12 - Design Patterns

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.

Solution 13 - Design Patterns

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

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

Solution 14 - Design Patterns

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.

Solution 15 - Design Patterns

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.

Solution 16 - Design Patterns

> 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondevoured elysiumView Question on Stackoverflow
Solution 1 - Design PatternsawshepardView Answer on Stackoverflow
Solution 2 - Design PatternsEdwin BuckView Answer on Stackoverflow
Solution 3 - Design PatternsRavindra babuView Answer on Stackoverflow
Solution 4 - Design PatternsVladimir KorneaView Answer on Stackoverflow
Solution 5 - Design PatternsJ. BurchView Answer on Stackoverflow
Solution 6 - Design PatternsMike BurtonView Answer on Stackoverflow
Solution 7 - Design PatternsPremrajView Answer on Stackoverflow
Solution 8 - Design PatternsChris LercherView Answer on Stackoverflow
Solution 9 - Design PatternsPinView Answer on Stackoverflow
Solution 10 - Design PatternswyldebillView Answer on Stackoverflow
Solution 11 - Design PatternsMichael FingerView Answer on Stackoverflow
Solution 12 - Design PatternsArianView Answer on Stackoverflow
Solution 13 - Design PatternsAvik ChowdhuryView Answer on Stackoverflow
Solution 14 - Design PatternsArun GhoshView Answer on Stackoverflow
Solution 15 - Design PatternsAli BayatView Answer on Stackoverflow
Solution 16 - Design Patternsmexique1View Answer on Stackoverflow