Using the Adapter Design Pattern in Java
The adapter pattern(structural design pattern) allows two unrelated interfaces to work together. I assume that your company has built an Human Resource Management application. An employee can be an Engineer/Senior Engineer/Manager. And your company just buy a library that support this application about Salary Management. But this library calls Engineer as Junior. Engineer has a showInfo() method but Junior has a ShowEmployeeInfo(). If we don't use Adapter pattern then whenever we want to show the employee information, we have to make an if statement if object is Engineer then object.showInfo() else object.ShowEmployeeInfo(); It makes the code become complex. We apply Adapter pattern to solve this problem. Steps to implements 1. Create a JuniorToEngineerAdapter class extends from Employee class 2. Before creating JuniorToEngineerAdapter above, we also create: - Employee, Engineer classes for my application. The Employee class contains showInfo() methods. Engineer class extends Employee - Junior class to simulate for the third party library The Junior contains showEmployeeInfo() methods 3. Create a demo class that manage a list of employee
Download
0 formatsNo download links available.