Adapter Pattern — get the gist in 2 min.

aditya chaudhari
JavaDeveloperDiary — JDD
2 min readAug 9, 2022

Do you want to make your Cat behave like a Lion ?? Think about the Adapter pattern because Adapter lets you convert the interface of one object (using adopter class) so another interface can understand it.
The intent of the Adapter is to alter an interface/interfaces so that it matches with what client is expecting.

This is done in a simple way where the adapter class implements the interface of one object and wraps another object. That is, implements the interface of a client class and wraps the adaptee class.

Simple Example :
Consider in your e-commerce application you have two payment gateways, and OLD legacy and new advanced both are not related to each other.

OLD legacy has below interface definition :
void doPayment(Account account1, Account account2);

New payment has below interface definition :
void makePayment(String mobile1, String mobile2);

The requirement is to migrate from the OLD payment gateway to the advanced new payment gateway, the client will use the new payment gateway but the program adopts the old gateway’s business logic while serving the client with the new payment gateway.

Solution Design and code example :

We have two interfaces that are not compatible with each other, AdvancedPayGateway and PaymentGateway . In our case client is going to use the new AdvancedPayGateway but internally we want to use the PaymentGateway to do the actual account transfer. Let’s see how we can achieve this.
Below is the solution UML for actual code please refer here .

adapter pattern java explained.

Components :

  1. Client class: Interacts with Adapter class.
  2. Target class/interface: This is a new class that client wants to use.
  3. Adapter Class: This inherits the Target and uses the adoptee object to use inside adapters class business logic.
    This adopts the interface of Adaptee to the Target interface.
  4. Adaptee class: This has the existing functionality which is expected to reuse in the adapter class. This defines the existing interface that needs adapting.

There are two ways to implement the adaptar pattern, class adapter and object adaptor.
Class adapter pattern is achieved using multiple inheritance , however in object adapter its achieved using object composition .
Above example is object adaptor pattern example.

The real fun is in the details, if you wish to know and understand the pros-cons, java-code example for the command pattern then please consider checking the detailed article at [adapter-pattern-explained-4-min-read]

Thanks for reading. Love IT, Live IT, Enjoy IT.

refer here for github code for above simple example.

On the journey to understand the core essence of design patterns.
Every Tuesday I publish a small 2 min post and a long 5–10 min post along with a code example, please consider follow if you wish to get notified!

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT