🚀 Enjoying Java Tutorials?
Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!
🔔 Subscribe to Ram N JavaWhat is the Adapter Design Pattern?
The Adapter Design Pattern falls under the category of Structural Design Patterns. It acts as a bridge between two incompatible interfaces, enabling them to communicate and work together seamlessly.
In software development, this pattern introduces an intermediate class called an Adapter. The adapter is responsible for translating requests from one interface format into a format that the target interface expects, without modifying the underlying source code of either system.
Understanding the Problem: Incompatible Systems
Consider an integration between an existing Human Resource (HR) System and a Third-Party Billing System:
- Client (HR System): Manages employee records and maintains employee data as a
String[](String array). - Adaptee (Third-Party Billing System): Contains a
processSalary()method designed to calculate salaries and deposit payments into employee bank accounts. However, this method only accepts anArrayList<Employee>.
Because the HR System outputs a String[] while the Billing System requires an ArrayList<Employee>, the HR System cannot make a direct method call to the billing system. Modifying either legacy system is often impossible or undesirable.
How the Adapter Bridges the Gap
By placing an Adapter between the HR System and the Third-Party Billing System:
- The HR System passes the employee data in its native
String[]format to the adapter. - The Adapter reads the array, constructs individual
Employeeobjects, and packages them into anArrayList<Employee>. - The Adapter then invokes the
processSalary()method on the Third-Party Billing System using the newly formed list. - The Billing System processes the payroll and deposits salaries as expected.
Pattern Role Breakdown
| Pattern Role | Component in Example | Responsibility |
|---|---|---|
| Client | HR System | Provides employee data as String[] |
| Adapter | Employee Adapter | Converts String[] into ArrayList<Employee> |
| Adaptee | Third-Party Billing System | Processes salary using ArrayList<Employee> |
Related Tutorials from Ram N Java
Click here to watch in Youtube : https://www.youtube.com/watch?v=YYC8_HOAFKY
See also: