JavaScript – Adapter Pattern

GitHub Code

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.

To run:
▪    Make GET call

Screen Shot 2017-04-30 at 2.40.26 PM
▪    Review output

Screen Shot 2017-04-30 at 2.57.16 PM

The ‘components’ (reference #1) are a Client, ITarget, Adapter, Adaptee.  In my implementation, I have 4 files.
Screen Shot 2017-04-30 at 2.51.48 PM

Specifics:

  • Adaptee – Adaptee.js – This is another system that has nothing to do with the client.  Its data is returned via getAdapteeData().

Screen Shot 2017-04-30 at 2.57.53 PM

  • 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.

Screen Shot 2017-04-30 at 2.57.46 PM

  • ITarget – ITarget.js – Interface for getTargetData().

Screen Shot 2017-04-30 at 2.57.37 PM

  • 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.

Screen Shot 2017-04-30 at 2.57.27 PM

Stay tuned!

References
1.    http://www.blackwasp.co.uk/Adapter.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s