What is the difference between the Facade and Adap

2019-03-08 00:12发布

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

Thanks

13条回答
你好瞎i
2楼-- · 2019-03-08 00:32

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.

查看更多
The star\"
3楼-- · 2019-03-08 00:35

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.

查看更多
Luminary・发光体
4楼-- · 2019-03-08 00:41

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.

查看更多
闹够了就滚
5楼-- · 2019-03-08 00:45

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.

查看更多
We Are One
6楼-- · 2019-03-08 00:51

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.

查看更多
对你真心纯属浪费
7楼-- · 2019-03-08 00:51

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                           |
+--------------------------------------------------------------+-----------------------------------------------+
查看更多
登录 后发表回答