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.

I. OVERVIEW

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.

II. PRACTICE
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:

2.2- Create ICustomer interface:

2.3- Create PersonToCustomerAdapter class that implements ICustomer interface:

2.4- Create Client Test Class:

2.5- Run the code, the console window shows:

III. SOURCE CODE

AdapterPattern


Related Posts


Got Something To Say:

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

*