Sunday, April 5, 2015

The Adapter Pattern: Plug and Play

I have a soft spot for the adapter pattern when I am teaching any of the twenty three Gang of Four design patterns. I guess I really like it because it is so simple to grasp. And it demonstrates designing to the abstraction, a feature that almost seventy percent of these design patterns share.

Almost every mobile phone and table sold today uses a USB cable to power and charge it. I can find a variety of adapters to connect that USB cable to different styles power outlets in the United States or other countries, or to connect it to a 12-volt automotive system. Of course some of these adapters work better than others, so getting one from a questionable source may put my device at risk. But being able to substitute any adapter that provides me with a USB port is what the adapter pattern is all about.