What is the difference between a facade template and an adapter?

I read both definitions and they seem to be exactly the same. Can anyone indicate what their differences are?

thank

+84
java c # design-patterns adapter facade
Jun 02 '10 at 20:21
source share
14 answers

This wiki page on the wiki page contains a brief note about this.

"An adapter is used when the wrapper must respect a particular interface and must support polymorphic behavior. On the other hand, a facade is used when a simpler or simpler interface is required to work with."

I heard the analogy that you should think about your universal remote control that you have configured to work with all your stereo systems - you press "on" and it turns on your cable, your receiver and TV, Maybe it’s really fancy home theater, and it dims the light and paints. This Facade is a single button / function that performs a more complex set of steps.

The adapter template simply connects two incompatible interfaces.

EDIT: A quick analogy of an adapter template (comment-based) could be something like a DVI-to-VGA adapter. Modern graphics cards are often DVI, but you have an old VGA monitor. With an adapter that connects to your video card, a DVI input is expected and has its own VGA input, you can get your old monitor to work with the new video card.

+114
Jun 02 '10 at
source share

The adapter == turns the square pin into a round hole.

Facade == separate control panel for starting all internal components.

+100
Jun 02 '10 at 20:36
source share

Honestly, many templates can be implemented equally programmatically - the difference in intention.

The adapter design pattern is designed to “translate” the interface of one or more classes into the interface that the client expects to use — the adapter translates calls to the expected interface into the actual interface that the wrapped classes use.

The Facade pattern is used when a simpler interface is required (and, again, can be implemented in the same way by wrapping intruder classes.) You would not say that you use a facade when the existing interface is incompatible, just when you need to make it more readable less poorly designed, etc.

+18
Jun 02 '10 at 20:39
source share

The facade is designed to organize several services behind a single service gateway. The adapter is designed to provide the ability to use a known interface to access the unknown.

+11
Jun 02 '10 at 20:30
source share

Facade:

Key findings: (from journaldev article by Pankaj Kumar)

  1. The facade template is more like an assistant for client applications
  2. The facade template can be applied at any point in development, usually when the number of interfaces grows and the system becomes complex.
  3. The interfaces of the subsystems do not know about the Facade, and they should not have any links to the interface of the Facade
  4. The facade template should be used for interfaces of a similar type, its purpose is to provide one interface, and not several interfaces that perform similar types of work

Facade class diagram:

enter image description here

Adapter:

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

Adapter class diagram:

enter image description here

You can find more information about the adapter in this SE post:

Difference between bridge pattern and adapter pattern

Key differences:

  1. The façade defines the new interface, while Adapter uses the old interface. The adapter allows two existing interfaces to work together, rather than defining a completely new
  2. Adapter and Facade are both wrappers; but these are different types of wrappers. Facade's goal is to create a simpler interface, and Adapter's goal is to develop an existing interface

Check out the sourcemaking article for a better understanding.

+10
Jun 05 '16 at 17:48
source share

The façade usually contrasts with the 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 | +--------------------------------------------------------------+-----------------------------------------------+ 
+5
Jan 27 '18 at 13:16
source share

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

  • The facade is used to encapsulate the entire layer and offers some ways to access it “conveniently”.
  • An adapter is used in which you have two components that should work together, but not so, just because of some "minor" differences in the interface.
+3
Jun 02 '10 at 20:34
source share

I will try to explain this in simple words, without much formality.

Imagine that you have some domain classes and user interface that you want to interact with. The facade can be used to provide functions that can be called from the user interface layer so that the user interface layer is not aware of any domain classes other than the facade. This means that instead of calling functions in domain classes, you call one function from the facade, which will be responsible for calling the necessary functions from other classes.

An adapter, on the other hand, can be used to integrate other external components that may have the same functionality, but their functions are not called exactly the same. Say that you have a Car class in your domain and you are working with an external car supplier who also has a Car class. In this class, you have the car.getDoors() function, but the external provider has the equivalent of car.getNumDoors() . You do not want to change the way this function is called, so you can use the adapter class to transfer the Car outer class so that the adapter getDoors() call is getDoors() to the outer class getDoors() call.

+3
Jun 03 '10 at 7:38
source share

goal

the facade is simplicity

compatibility adapter.

+3
Mar 19 '18 at 2:04
source share

The adapter template allows two previously incompatible interfaces to work with each other. It has 2 separate interfaces in the game.

The Facade template adopts a well-known interface that is low / fine-grained and wraps it with a higher-level / of course grainy interface. It has one interface that has been simplified by porting to another.

+2
Jun 02 '10 at 20:33
source share

The adapter combines two interfaces.

The facade exposes one class to a higher and more limited level. For example, the facade of a view model can display only certain read-only properties of a lower-level class.

+2
Jun 02 '10 at 20:34
source share

I read both definitions and they seem to be exactly the same.

Really?

I noticed that the term Adapter is sometimes used to describe what Stategy really is, maybe because the word is more expressive.

For example, in the Zend Framework, all Adapter classes are actually implementations of the Strategy template, because they only carry their own code behind the classes in order to have several behaviors.

Adapters are often used to port old or old code.

0
Jun 04 2018-10-18T00-06-04
source share

Facade

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

adapter

These are analogs of hardware adapters. Say you want to connect a USB device to a serial port , you need a USB-serial port adapter .

0
Aug 08 '18 at 8:50
source share

The adapter template connects two incompatible interfaces, providing a new interface.

The facade template simplifies a complex subsystem (having several components) with a single interface.

0
Aug 12 '19 at 21:43 on
source share



All Articles