What is the difference between the Facade and Adapter Pattern?

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

    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.

提交回复
热议问题