Adapter Pattern in Java

Adapter Pattern is a Structural Pattern in which, an interface of a class is converted into another interface that Client expects to work. With Adapter Pattern, we can reuse existing code without changing it.


Adapter Pattern defines an Adapter that can adapt a target class/interface (that we called Adaptee) to Client‘s requirement.

The Adapter contains an instance of Adaptee (also hides it from Client). It helps that, Client calls Adapter methods without knowing anything about Adaptee, then Adapter uses Adaptee instance inside to call its appropriate methods.


1. Project Overview

We have Person ‘fullname’ information, but Client want to use ‘first name’ and ‘last name’ as Customer information. So we create a Person-to-Customer-Adapter that Client will work with.

2. Step by Step

2.1- Create Person class:

package com.javasampleapproach.adapter.pattern;

public class Person {
	private String fullName;

	public Person(String fullName) {
		this.fullName = fullName;

	public String getFullName() {
		return fullName;

	public void setFullName(String fullName) {
		this.fullName = fullName;

	public String toString() {
		return "Person [fullName=" + fullName + "]";

2.2- Create ICustomer interface:

package com.javasampleapproach.adapter.pattern;

public interface ICustomer {

	public String getFirstName();
	public String getLastName();

2.3- Create PersonToCustomerAdapter class that implements ICustomer interface:

package com.javasampleapproach.adapter.pattern;

public class PersonToCustomerAdapter implements ICustomer {

	private Person person;

	public PersonToCustomerAdapter(Person person) {
		this.person = person;

	public String getFirstName() {
		return this.person.getFullName().split(" ")[0];

	public String getLastName() {
		return this.person.getFullName().split(" ")[1];


2.4- Create Client Test Class:

package com.javasampleapproach.adapter;

import com.javasampleapproach.adapter.pattern.ICustomer;
import com.javasampleapproach.adapter.pattern.Person;
import com.javasampleapproach.adapter.pattern.PersonToCustomerAdapter;

public class MainApp {

	public static void main(String[] args) {

		Person person = new Person("Jack Smith");

		ICustomer adapter = new PersonToCustomerAdapter(person);

		System.out.println("--- Customer Information ---");
		System.out.println("First Name: " + adapter.getFirstName() + " | Last Name: " + adapter.getLastName());

2.5- Run the code, the console window shows:

Person [fullName=Jack Smith]
--- Customer Information ---
First Name: Jack | Last Name: Smith



By grokonez | March 16, 2017.

Last updated on May 3, 2021.

Related Posts

Got Something To Say:

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