Skip to content
APPCITORThe Tech Blog
Esc

Structural – Adapter Design Pattern

August 1, 2020 1 min read

Structural – Adapter Design Pattern

Adapter pattern is a Structural Design Pattern that allows 2 incompatible interfaces to work together according to the client’s requirement without changing the existing code.

Adapter Design Pattern is also known as Wrapper.

Below given is an example in java to understand about adapter pattern.

Let’s assume that you are short on toycat object and you would like to use animal objects in their place. Animals have some similar functionality but implement a different interface, so we can’t use them directly. For that we use adapter pattern. Here our client would be Toycat and adaptee would be animal.

First thing we are going to do is creating 2 interfaces that we are targeting to.

Here animals implement “Animal” interface that allows them to eat and make sounds adaptee interface from the methods “eat()” and “makeSound()”. Inside the class “Lion” that is implemented by the adaptee interface, you can see a concrete implementation of animal.

“ToyCat” is the target interface. Toycats don’t eat. They just make “meow” sound. We should implement “AnimalAdapter” class that your client expects to use from the “Toycat” interface. And from the constructor created inside the class gives the reference to the object we are adapting. Inside the overridden method “Meow” translates the methods appropriately. Let’s go to the main method.

Here we needed to wrap an animal in an AnimalAdapter so that it behaves like toy cat. So we called the constructor “AnimalAdapter” by the reference made from “ToyCat” interface. You can see toy cat behaving like an animal from the method “Meow” calling by the adapter object reference.

You can visit the below given articles to learn about creational design patterns.

Thank you.

Creational Design Patterns

Singleton Design Pattern

Factory Design Pattern

Abstract Factory Design Pattern

Prototype Design Pattern

Builder Design Pattern

Share this article

Keep reading