stock market

Adapter Design Pattern in Java with UML Diagram and Real Use Cases

Spread the love

We have an existing object which provides the functionality that client needs , but client code can not use this object because its expects an object with different interface .

Using adapter design pattern we make this object work with client by adopting the object to client expects interface .

this pattern is also called as wrapper as its wrap existing objects .

Adapter design pattern is used so that two unrelated interface can work together, the object that joins these unrelated interface is called as adapter .

Adapter design pattern we can implement using

  • Class Adapter
  • Object Adapter

1. Class Adapter : this form uses java Inheritance and extends the source interface

Adaptee: our existing class providing needed functionality

Client : Needs functionality provided but as different interface than adaptee .

Target : target interface expected by client

Adapter : Adapter adapts existing functionality to target interface .

1. Adaptee class


package com.test.rkdigital.school.Adapter;
public class Employee {

	private String firstName; 
	private String jobtitle;
	private String officeLocation;
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getJobtitle() {
		return jobtitle;
	}
	public void setJobtitle(String jobtitle) {
		this.jobtitle = jobtitle;
	}
	public String getOfficeLocation() {
		return officeLocation;
	}
	public void setOfficeLocation(String officeLocation) {
		this.officeLocation = officeLocation;
	}
}

2. Target Interface

package com.test.rkdigital.school.Adapter;
public interface Customer {
public String getName();
public String getDesignation();
public String getAddress();
}

package com.test.rkdigital.school.Adapter;

public class BusinessCardDesigner {

public String designCard(Customer customer) {

String card="";

card+= customer.getName();

card+="\n" +customer.getDesignation();

card+="\n" +customer.getAddress();

return card;

}
}

3.Adapter Class

package com.test.rkdigital.school.Adapter;

public class EmployeeClassAdapter extends Employee implements Customer{

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return this.getFirstName();
	}
	@Override
	public String getDesignation() {
		// TODO Auto-generated method stub
		return this.getJobtitle();
	}
	@Override
	public String getAddress() {
		// TODO Auto-generated method stub
		return this.getOfficeLocation();
	}	
}

4. Main class

package com.test.rkdigital.school.Adapter;

public class MainTest {
	public static void main(String[] args) 
	{
		EmployeeClassAdapter adapter=new EmployeeClassAdapter();
		populateEmployeeData(adapter);
		BusinessCardDesigner designer =new BusinessCardDesigner();
		String card=designer.designCard(adapter);
		System.out.println(card);
	}
	private static void populateEmployeeData(Employee employee)
	{
		employee.setFirstName("Rakesh Kumar");
		employee.setJobtitle("software security");
		employee.setOfficeLocation("Navi mumabi");
	}

}

2. Object Adapter: This form uses java composition and adapter contains the source object .

1. Objectadapter class

package com.test.rkdigital.school.Adapter;

public class EmployeeObjectAdapter implements Customer{

	private Employee adaptee;

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return adaptee.getFirstName();
	}

	@Override
	public String getDesignation() {
		// TODO Auto-generated method stub
		return adaptee.getJobtitle();
	}

	@Override
	public String getAddress() {
		// TODO Auto-generated method stub
		return adaptee.getOfficeLocation();
	}
	
	public EmployeeObjectAdapter(Employee adptee) {
		this.adaptee=adptee;
	}
}

2. Object Main class

package com.test.rkdigital.school.Adapter;

public class ObjectAdapterMainTest {
	public static void main(String[] args) {
		Employee employee=new Employee();
		populateEmployeeData(employee);
		EmployeeObjectAdapter objectadapter=new EmployeeObjectAdapter(employee);
		BusinessCardDesigner designer =new BusinessCardDesigner();
		String card=designer.designCard(objectadapter);
		System.out.println(card);
	}

	private static void populateEmployeeData(Employee employee)
	{
		employee.setFirstName("Rakesh Kumar");
		employee.setJobtitle("software security");
		employee.setOfficeLocation("Navi mumabi");
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *