Click here to watch in Youtube : https://www.youtube.com/watch?v=0WDACH00TFk
Click the below Image to Enlarge
Adapter Pattern – Implementation [Class Adapter] |
Adapter Pattern – Implementation [Class 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 + "]";
}
}
{
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");
}
}
}
}
/*
* 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);
}
{
public void processCompanySalary(String[][] employeeInfo);
}
EmployeeAdapter.java
import java.util.ArrayList;
public class EmployeeAdapter extends ThirdPartyBillingSystem implements ITarget
{
/*
* 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 call the processSalary method inherited from the ThirdPartyBillingSystem for processing the employee salary");
processSalary(employeeList);
}
}
public class EmployeeAdapter extends ThirdPartyBillingSystem implements ITarget
{
/*
* 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 call the processSalary method inherited from the ThirdPartyBillingSystem for processing the employee salary");
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());
}
}
* 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 call the processSalary method inherited from 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
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 call the processSalary method inherited from 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 [Object Adapter]
Adapter Design Pattern - Implementation [Mobile Charger]
Adapter Design Pattern - Key Points
All Design Patterns Links
No comments:
Post a Comment