Adapter Pattern
Imagine you are putting your hands into a project that has been running for a few years now and you are about to update some of its code. It depends on the occasion but most of the time what you’ll need is to cut a few lines of code and make it work the way you want it to. Although this might seem like the best idea due to the small amount of code you’re replacing and the time you’ll spend, it is best suggested to implement an adapter, a man in the middle if you may.
Get Iacobus’s stories in your inbox
Join Medium for free to get updates from this writer.
The Adapter Pattern dictates that instead of editing code despite the easiness and the speed when doing so, you should create an «adapter», an object that will help you communicate object A with object B without modifying the code, logic or properties (respectively) that were there before. And the reason is because in the real world robust applications will be dependent on external libraries, APIs or just other people’s snippets and these usually get to be updated, thus conflicting with the modified source code.
Also, modifying customized code (Adapter) that is not from an external source is preferable because it serves your isolated and unique situation.
Adapter Pattern [UML] (during execution).
- objectA
+ methodA() [calls objectAdapter’s ‘methodX’]- objectAdapter *
+ methodX [calls objectB’s ‘methodB’and or ‘methodC’]- objectB (External class)
+ methodB
+ methodC









