What is the difference between the Facade and Adapter Pattern?

后端 未结 16 913
孤城傲影
孤城傲影 2021-01-29 23:50

I\'ve been reading both definitions and they seem quite the same. Could anyone point out what are their differences?

Thanks

相关标签:
16条回答
  • 2021-01-30 00:28

    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.

    0 讨论(0)
  • 2021-01-30 00:29

    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.

    0 讨论(0)
  • 2021-01-30 00:30

    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.
    0 讨论(0)
  • 2021-01-30 00:31

    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.

    0 讨论(0)
  • 2021-01-30 00:36

    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:

    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:

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

    Difference between Bridge pattern and Adapter pattern

    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.

    0 讨论(0)
  • 2021-01-30 00:37

    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                           |
    +--------------------------------------------------------------+-----------------------------------------------+
    
    0 讨论(0)
提交回复
热议问题