David
David

Reputation: 81

Best design pattern to use: adapter or facade

I can't decide on which pattern is best suited to the following problem.

I have a client system who will be interacting with a separate subsystem. The subsystem is quite complicated and so I need an interface between the two to simplify the client system. This sounds like a perfect fit for Facade pattern but I think Adapter pattern too fits for my problem.

Does it make any difference if the interface in the middle calls individual tasks on the subsystem through simple API calls?

Upvotes: 8

Views: 6241

Answers (6)

Ravindra babu
Ravindra babu

Reputation: 38910

Facade pattern (An object that provides a simplified interface to a larger body of code) fits for your use case.

Checklist to use Facade: ( From linked Wikipedia article)

  1. A simple interface is required to access a complex system.
  2. The abstractions and implementations of a subsystem are tightly coupled.
  3. Need an entry point to each level of layered software.
  4. System is very complex or difficult to understand.

Related SE question for more details on Facade.

What is Facade Design Pattern?

Even though both Facade and Adapter are structural patterns, the intent is different( samitgaur answer explained the intent part nicely).

Since you are not converting one interface to other interface, Adapter does not serve your purpose.

Related SE question:

What is the difference between the Facade and Adapter Pattern?

Upvotes: 0

Meet B
Meet B

Reputation: 25

Facade deals with interface, not implementation. Its purpose is to hide internal complexity behind a single interface that appears simple on the outside.

Upvotes: -3

samitgaur
samitgaur

Reputation: 5661

Adapter pattern is used when you want to adapt an existing class's interface to another interface that a client expects to work with. It usually just involves delegation or translation from a method of one interface to the corresponding method of the other.

Facade is used when you want to simplify a complex system by exposing a simpler set of APIs that a client can work with. It involves translating a complex pattern of API calls into a single API call.

Your case sounds more like you need a facade than an adapter. Implementing just the adapter pattern will not give you the benefit of API simplification. In the end, it doesn't matter what you call it. And these patterns are not exclusive. You could mix both in a way that gives you the most benefit.

Upvotes: 7

krolth
krolth

Reputation: 1032

The difference between Facade and Adapter is mostly the intent.

If what you want to do is simplify the interface then you are looking at a Facade. If you want to Adapt the interface so that it can be used as something else then its an Adapter.

But really, what is the problem how you call it? My rule of thumb is if you are implementing an existing interface you probably are using the Adapter interface. If you are creating a new simplified interface its a Facade.

Upvotes: 0

Paul Sasik
Paul Sasik

Reputation: 81429

This clearly seems to be a Facade pattern case your goal is simplification rather than an actual adaptation.

Façade Definition:

Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

Adapter Definition:

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Definitions lifted from: http://dofactory.com/Patterns/Patterns.aspx

Upvotes: 6

heisenberg
heisenberg

Reputation: 9759

From your description its more in-line with the accepted definition of the Facade, but I'd say its more of a semantic debate than anything else. Facade in general is more reducing the complexity of interfacing with an entire subsystem, whereas adapter is more geared towards tweaking an existing interface or call to your specific needs (eg the basic functionality is there but the return types aren't quite what you want, etc).

Upvotes: 9

Related Questions