Editorials

The Adapter Pattern to the Rescue

I have been reviewing reader comments on recent editorials about select software building blocks. Some were ok with using open systems frameworks, others preferred third party commercial tools, while others simply liked to use the tools shipping from the vendor of the software environment directly.

The main concern was that any selection could become obsolete, lose support for future upgrades, or have bugs that are fixed too slowly. The goal was to find one tool for a process such as logging, and implement it fully, betting the bank on having made a choice that will withstand the test of time.

Ultimately we end up saying, “help Mr. Wizard!”, because no software library lives on in a static form. We’re always trying to make things better, giving people a reason to purchase our latest/greatest gadget.

This is where the adapter pattern can come to the rescue. Using the adapter pattern you create an adapter between the external library and your code. Instead of calling the library directly, you can call methods implemented in the adapter, that then make the call to the third party products.

This allows you to simply create an adapter for any library you want to introduce into your application rather than changing ever call throughout your app to external libraries. All the external calls are in one place, the adapter.

Using the adapter you have the flexibility to have multiple adapters over time, and your software has a sense of timelessness. A new, ever better library is released and you introduce it into your application by creating a new adapter. Using the adapter you reduce the risk of making a bad choice in an external library, or even rolling your own.

Creating an interface for your adapter makes it even easier to replace one implementation with another. It adds a little work up front, but results in a greatly simplified introduction of a new or modified adapter at a later time.

From a practical perspective you could easily substitute a self designed ORM with NHibernate, Entity Framework or even Linq2Sql.

Share your experience with Adapters here online, or drop an email to btaylor@sswug.org.

Cheers,

Ben

Note: Watch this cartoon on YouTube made in the 1970s if you’re interested in the nuance behind the phrase “Help Mr. Wizard”.