Adapter Pattern in Java

Arjun Sunil Kumar
Software Engineering
2 min readAug 18, 2018

As the name suggests, adapting the current class to work with another class. This is what an adapter does.

Use Case:

  1. Healthcare Machinery :
Doing a data analysis on X-Ray data.

Say, you are building a health care software. You get data from different type of X-Ray Machine(ie different manufactures). Your program is supposed to do Data Analysis on Health Care Machine data.You software is programmed to work on Cisco X-Ray Machine. But, at later point of time, you need the software to adapt to IBM X-Ray Machine. What should you do?

You create a interface(Literal meaning) to handle the data conversion, data retrieval etc, so that software can interact with IBM X-Ray Machine in a standard way. You are creating a Global Template and then creating interfaces(Literal meaning) for different manufacturers of X-Ray Machine.

2. Analog to Digital:

Adapter

Say we have an analog sensor, which outputs analog signals. But our program can only interact with digital signals. Hence we create an adapter (AnalogToDigital ) to convert analog to digital.

3. Apple created a whole system of Adapters, to make it work on Intel Processors.

X-Ray machine Example

Since we need to perform data analysis on multiple dataset, we need adapters for various manufactures.

In the end, every manufacture data set will be likely of the same format, but there might some variations in terms of unit of measurement (Kg/Pound or °F /°C ) etc. So we need to standardize this values, so that our program can expect the same kind of data set from all the manufactures.

Project Structure:

Implementation in Java

  1. We have interfaces(Literal meaning) for different manufactures. They provide us with functions to collect respective data.

Cisco_XRay.java

IBM_XRay.java

2. Let create a standard template for X-Ray machine.

ITarget.java

Concretion Adapter for the above Interface.

IBMAdapter.java

CiscoAdapter.java

3. Driver class to test this.

Found it Interesting?
Please show your support by 👏. To read the complete series, click below.

Disclaimer:
I myself, has just started learning, design patterns. If you find any issues please feel free to post them in the comments section below. Thank you for reading so far 😄

--

--

Arjun Sunil Kumar
Software Engineering

Writes on Database Kernel, Distributed Systems, Cloud Technology, Data Engineering & SDE Paradigm. github.com/arjunsk