The Adapter pattern is meant to be a conduit between two unrelated classes or systems. More specifically, if system A needs access to system B, but doesn’t accept system B’s public interface, an ‘adapting’ interface/class is created to take what system A needs from system B and put it in a format system A can understand.
This pattern sounds a bit like a band aid. I am assuming (knowing what I know about this pattern now), I would redo either system A or B to accomplish the task without an adapter. I would only use an adapter if I couldn’t ‘redo’ system A and/or B for some reason.
▪ Make GET call
▪ Review output
The ‘components’ (reference #1) are a Client, ITarget, Adapter, Adaptee. In my implementation, I have 4 files.
- Adaptee – Adaptee.js – This is another system that has nothing to do with the client. Its data is returned via getAdapteeData().
- Adapter – Adapter.js – This is the class implementing ITarget.js. It has a reference to adaptee. The method getAdapteeData() from within getTargetData() when it is called from the client.
- ITarget – ITarget.js – Interface for getTargetData().
- Client – adapterDriver.js – I am using the normal driver class as the client. Inside the client:
- adapter is created
- adaptee is created and placed within adapter.
- adapter.getTargetData() is called and the adaptee data is returned in a format the client can understand.