Thursday 7 November 2013

Adapter Pattern – Implementation [Object Adapter]


Click here to watch in Youtube : https://www.youtube.com/watch?v=TMFJ1Zy2sDc

Click the below Image to Enlarge

Adapter Pattern – Implementation [Object Adapter]





















Adapter Pattern – Implementation [Object Adapter] - Class Diagram


























Employee.java

public class Employee
{
private int    empId;
private String name;
private String designation;

public Employee( int empId, String name, String designation )
{
super();
this.empId = empId;
this.name = name;
this.designation = designation;
}

public int getEmpId()
{
return empId;
}

public void setEmpId( int empId )
{
this.empId = empId;
}

public String getName()
{
return name;
}

public void setName( String name )
{
this.name = name;
}

public String getDesignation()
{
return designation;
}

public void setDesignation( String designation )
{
this.designation = designation;
}

@Override
public String toString()
{
return "Employee [empId=" + empId + ", name=" + name + ", designation=" + designation + "]";
}

}

ThirdPartyBillingSystem.java

import java.util.ArrayList;

/*
 * ThirdPartyBillingSystem accepts employees information as a List to process each employee salary
 */
public class ThirdPartyBillingSystem
{
public void processSalary( ArrayList<Employee> employeeList )
{
for( Employee employee : employeeList )
{
System.out.print("\n"+employee.toString()+" : ");
if( employee.getDesignation().equalsIgnoreCase("Team Leader") )
{
System.out.println("70000Rs Salary credited to " + employee.getName() + " Account\n");
}
else if( employee.getDesignation().equalsIgnoreCase("Developer") )
{
System.out.println("40000Rs Salary credited to " + employee.getName() + " Account\n");
}
else if( employee.getDesignation().equalsIgnoreCase("Tester") )
{
System.out.println("30000Rs Salary credited to " + employee.getName() + " Account\n");
}
}
}

}

ITarget.java

public interface ITarget
{
public void processCompanySalary(String[][] employeeInfo);
}


EmployeeAdapter.java

import java.util.ArrayList;

public class EmployeeAdapter implements ITarget
{
ThirdPartyBillingSystem thirdPartyBillingSystem =new ThirdPartyBillingSystem();

/*
* This Adapter method converts String Array employee information to ArrayList of Employees
*
*/

@Override
public void processCompanySalary( String[][] employeeInfo )
{
String empId = null;
String name = null;
String designation = null;
ArrayList<Employee> employeeList = new ArrayList<Employee>();
for( int i = 0; i < employeeInfo.length; i++ )
{

for( int j = 0; j < employeeInfo[i].length; j++ )
{
if( j == 0 )
{
empId = employeeInfo[i][j];
}
else if( j == 1 )
{
name = employeeInfo[i][j];
}
else
{
designation = employeeInfo[i][j];
}
}
employeeList.add(new Employee(Integer.parseInt(empId), name, designation));

}

System.out.println("Adapter converted Array of Employee to ArrayList of Employee : \n"+employeeList+"\n"+
"then delegate to the ThirdPartyBillingSystem for processing the employee salary");
thirdPartyBillingSystem.processSalary(employeeList);


}

}


HumanResourceSystem.java

/*
 * Client System which has employee information as String Array
 *
 */

public class HumanResourceSystem
{

public static String[][] getEmployees()
{
String[][] employees = new String[4][];

employees[0] = new String[] { "100", "Dave", "Team Leader" };
employees[1] = new String[] { "101", "Ram", "Developer" };
employees[2] = new String[] { "102", "Raj", "Developer" };
employees[3] = new String[] { "103", "Rahul", "Tester" };

return employees;
}

public static void main( String args[] )
{

ITarget target = new EmployeeAdapter();
System.out.println("HR system passes employee string array to Adapter\n");
target.processCompanySalary(getEmployees());

}
}


Output

HR system passes employee string array to Adapter

Adapter converted Array of Employee to ArrayList of Employee :
[Employee [empId=100, name=Dave, designation=Team Leader], Employee [empId=101, name=Ram, designation=Developer], Employee [empId=102, name=Raj, designation=Developer], Employee [empId=103, name=Rahul, designation=Tester]]
then delegate to the ThirdPartyBillingSystem for processing the employee salary

Employee [empId=100, name=Dave, designation=Team Leader] : 70000Rs Salary credited to Dave Account


Employee [empId=101, name=Ram, designation=Developer] : 40000Rs Salary credited to Ram Account


Employee [empId=102, name=Raj, designation=Developer] : 40000Rs Salary credited to Raj Account


Employee [empId=103, name=Rahul, designation=Tester] : 30000Rs Salary credited to Rahul Account


See also:

  • Adapter Design pattern - Introduction
  • Adapter Design Pattern - Real Time Exmaple(Translator)
  • Adapter Design pattern - Real Time Example (Card Reader)
  • Adapter Design pattern - Real Time Example (Mobile Charger)
  • Adapter Design pattern - Real Time Example (Universal Adaptor)
  • Adapter Design pattern - Real Time Example [JDBC Driver]
  • Adapter Design pattern - Class diagram
  • Adapter Design Pattern - Sequence diagram
  • Adapter Design Pattern - Implementation [Language Translator]
  • Adapter Pattern - Implementation [Class Adapter]
  • Adapter Design Pattern - Implementation [Mobile Charger]
  • Adapter Design Pattern - Key Points
  • All Design Patterns Links
  • 3 comments:

    1. Its really helpful for me to understand. Thanks.
      If anyone wants to Learn Java in Chennai go to the Besant Technologies which is No.1 Training Institute in Chennai.

      ReplyDelete
    2. Hi,
      What are the disadvantages of using:
      EmployeeAdapter target = new EmployeeAdapter();
      intead of:
      ITarget target = new EmployeeAdapter();

      ?? at the end, in your java imports you will have to have EmployeeAdapter and ITarget. Even if you use maven or not.

      ReplyDelete
      Replies
      1. Yes you are correct .we can do like that also.I don't see any issues.

        Delete