eaglewap
FollowData Scientist | Software Engineer | Google Code U Alumni |
Former WSO2 Software Engineering Intern | "Google Code U"Alumni | Former WSO2 Software Engineering Intern.
No comments
Adapter Design Pattern
AKA wrapper
Use when an (external) component is not compatible with the current system
Convert the interface of a class into another interface that clients expect
–Wrap an existing class with a new interface
Lets classes work together, that could not otherwise because of incompatible interfaces
Purpose Permits classes with disparate interfaces to work together by creating a common object by which they may communicate and interact. Use When ??
A class to be used doesn’t meet interface requirements.
n Complex conditions tie object behavior to its state.
n Transitions between states need to be explicit.
Class diagram for Adapter Design Pattern
Adapter
Match interfaces of different classes
Example Java Code for Adapter Design Pattern 😊
There is a problem in two different interfaces. Where HR system proceeds the Employee details in a String array and Other Third party Billing system wants the Details in a Array list to proceed. This is the scenario where Adapter is needed in order to change the Array list employees to Employee class array list . Adapter changes it implementing the target interface. ITarget.java
publicclassThirdpartyBillingSystem{//Its given by third party //It can only process Array List of Employees//But we have Employee String ArraypublicvoidprocessSalary(ArrayList<Employee> emplyeelist){for(Employee employee : emplyeelist){
System.out.println(employee.toString()+":");if(employee.getDesignition().equalsIgnoreCase("Team Leader")){
System.out.println("70000Rs Salary credited to "+ employee.getName()+" Account\n");}elseif(employee.getDesignition().equalsIgnoreCase("Developer")){
System.out.println("40000Rs Salary credited to "+ employee.getName()+" Account\n");}elseif(employee.getDesignition().equalsIgnoreCase("Tester")){
System.out.println("30000Rs Salary credited to "+ employee.getName()+" Account\n");}}}}
Comments (0)
Post a Comment
Cancel