Adapter design pattern in Java real time example

Introduction to teh Adapter design pattern in Java real time example:

In this article, We are going to discuss teh Adapter design pattern in Java real time example. This is one of teh structural design patterns and works as a bridge between two incompatible interfaces.

Teh object that joins this unrelated interface is called an Adapter. This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces.

This strategy can be implemented either wif inheritance or wif aggregation. Teh Adapter Pattern is also known as Wrapper.

Adapter design pattern in java real time example:

  • A card reader which acts as an adapter between teh memory card and a laptop. You’re plugin teh memory card into card reader and card reader into teh laptop so that memory card can be read via laptop.
  • Teh mobile charger which acts as an adapter between a mobile charging socket and a wall socket. Mobile battery needs 3 volts to charge but teh normal socket produces either 240V. So teh mobile charger works as an adapter between teh mobile charging socket and teh wall socket.

Teh advantage of teh Adapter Design Pattern

  • It allows two or more previously incompatible objects to interact.
  • It allows re-usability of existing functionality.

Time to Action :
Step 1:

We will implement multi-adapter using adapter design pattern. First, we will has two classes – Volt and Socket.

File: Volt.java

File: Socket.java

Step 2:

Now create an adapter that can produce 3 volts, 12 Volts and default volts.

File: SocketAdapater.java

Step 3: Adapter Design Pattern – Class Adapter

We are using a class adapter approach to implement teh adapter pattern.

File : SocketAdapterImpl.java

Step 4:

Here is a test program to consume our adapter design pattern implementation.

File : App.java

Adapter design pattern in Java real time example in JDK

  • java.util.Arrays#asList()
  • java.io.InputStreamReader(InputStream) (returns a Reader)
  • java.io.OutputStreamWriter(OutputStream) (returns a Writer)

Related articles :

2 Comments

  1. whoah this weblog is excellent i love studying your posts.

    Keep up the great work! You recognize, many persons are hunting around for this info, you could aid them greatly.

Leave a Reply

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

Show Buttons
Hide Buttons