What is the difference between the Facade and Adapter Pattern?

后端 未结 16 912
孤城傲影
孤城傲影 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:37

    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.

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

    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.

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

    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.

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

    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.

    0 讨论(0)
提交回复
热议问题