Adapter Pattern




The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. 

Example  

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
public interface CRUD {
 public boolean create();
 public Object read();
 public boolean update();
 public boolean delete();
}
==========
public interface MVER {
 public boolean make();
 public Object view();
 public boolean edit();
 public boolean remove();
}
==========
public class Student implements CRUD{
 @Override
 public boolean create() {
  System.out.println("Creating student");
  return false;
 }

 @Override
 public Object read() {
  System.out.println("Reading student");
  return null;
 }

 @Override
 public boolean update() {
  System.out.println("updating student");
  return false;
 }

 @Override
 public boolean delete() {
  System.out.println("deleting student");
  return false;
 }
}
==========
public class Employee implements MVER{
 @Override
 public boolean make() {
  System.out.println("Making employee");
  return false;
 }

 @Override
 public Object view() {
  System.out.println("Viewing employee");
  return null;
 }

 @Override
 public boolean edit() {
  System.out.println("Editing employee");
  return false;
 }

 @Override
 public boolean remove() {
  System.out.println("Removing employee");
  return false;
 }
}
==========
public class EmployeAdapter implements CRUD{
 private Employee employee;
 EmployeAdapter(Employee employee){
  this.employee = employee;
 }
 
 @Override
 public boolean create() {
  return employee.make();
 }

 @Override
 public Object read() {
  return employee.view();
 }

 @Override
 public boolean update() {
  return employee.edit();
 }

 @Override
 public boolean delete() {
  return employee.remove();
 }
}
==========
public class AdapterFlight {
 public static void main(String[] bags){
  Employee employee = new Employee();
  employee.make();
  employee.edit();
  employee.view();
  employee.remove();
  
  EmployeAdapter employeeAdapter = new EmployeAdapter(employee);
  employeeAdapter.create();
  employeeAdapter.update();
  employeeAdapter.read();
  employeeAdapter.delete();
 }
}