Using Facades to Decouple API Integrations
The most important part of building an integration with an API is actually writing the code that will connect with the Web service and invoke its methods. I'll show you why using the Façade pattern to decouple calls from your existing code is a good idea and help you identify what kind of problems you might be able to prevent.
So, first things first, what is the Façade pattern?
A Façade is an object that provides simple access to complex - or external - functionality. It might be used to group together several methods into a single one, to abstract a very complex method into several simple calls or, more generically, to decouple two pieces of code where there's a strong dependency of one over the other.
There's a better way to keep API calls up-to-date. By writing a Façade with the single responsibility of interacting with the external Web service, you can defend your code from external changes. Now, whenever the API changes, all you have to do is update your Façade. Your internal application code will remain untouched.

From a Test Driven Development point-of-view, using a Facade offers a big advantage. You're now able to write simple tests against the Façade without affecting your internal code test results. By using this strategy, you'll be able to know immediately whenever an API is not working as you expected and make the necessary changes to the Façade.
This article is cross-posted at Using Facades to Decouple API Integrations.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Robert Morschel replied on Wed, 2013/03/20 - 8:53am
Interesting, because as well as an abstraction, decoupling layer, the facade allows things like throttinling and usage charging to be implemented without affecting the underlying services. This is the approach being promoted by 3rd party api management product vendors like Apigee, Layer 7, and WS02. Out of interest, do you have a preference for build vs buy in this area?
Robert
SOA API Management Product Evaluation