Wednesday, 18 June 2025

 

                                                                  Adapter design pattern

The Adapter design pattern is a structural pattern that allows the interface of an existing class to be used as another interface. It acts as a bridge between two incompatible interfaces, making them work together. This pattern involves a single class, known as the adapter, which is responsible for joining functionalities of independent or incompatible interfaces.

Let's understand this concept using a simple example:

Let's say you have two friends, one who speaks only English and another who speaks only French. You want them to communicate, but there's a language barrier.

  • You act as an adapter, translating messages between them. Your role allows the English speaker to convey messages to you, and you convert those messages into French for the other person.
  • In this way, despite the language difference, your adaptation enables smooth communication between your friends.
  • This role you play is similar to the Adapter design pattern, bridging the gap between incompatible interfaces.

Components of Adapter Design Pattern in Java

1. Target Interface

  • Description: Defines the interface expected by the client. It represents the set of operations that the client code can use.
  • Role: It's the common interface that the client code interacts with.

2. Adaptee

  • Description: The existing class or system with an incompatible interface that needs to be integrated into the new system.
  • Role: It's the class or system that the client code cannot directly use due to interface mismatches.

3. Adapter

  • Description: A class that implements the target interface and internally uses an instance of the adaptee to make it compatible with the target interface.
  • Role: It acts as a bridge, adapting the interface of the adaptee to match the target interface.

4. Client

  • Description: The code that uses the target interface to interact with objects. It remains unaware of the specific implementation details of the adaptee and the adapter.
  • Role: It's the code that benefits from the integration of the adaptee into the system through the adapter.

How Adapter Design Pattern works?

  • Client Request: The client initiates a request by calling a method on the adapter using the target interface.
  • Adapter Translation: The adapter translates or maps the client's request into a form that the adaptee understands, using the adaptee's interface.
  • Adaptee Execution: The adaptee performs the actual work based on the translated request from the adapter.
  • Result to Client: The client receives the results of the call, remaining unaware of the adapter's presence or the specific details of the adaptee.

 

Example :

1. Target Interface

 

package adapterdesignpattern;

 

public interface PaymentGateway {

              

                void pay(double amount);

 

}

 

2. Adaptee

 

package adapterdesignpattern;

 

public class PayPal {

 

               void send(double amount) {

                              System.out.println("Paid $" + amount + " via PayPal.");

               }

 

}

 

package adapterdesignpattern;

 

public class Stripe {

 

               void makePayment(int amount) {

                              System.out.println("Paid $" + amount + " via Stripe.");

               }

              

              

}

 

 

package adapterdesignpattern;

 

public class RozarPay {

 

               void doTransaction(String amount) {

                              System.out.println("Paid $" + amount + " via RozarPay.");

               }

 

}

 

3. Adapter

 

 

package adapterdesignpattern;

 

public class PayPalAdapter implements PaymentGateway {

 

              

               private PayPal payPal=new PayPal();

 

               @Override

               public void pay(double amount) {

                              payPal.send(amount);

                             

               }

              

              

}

 

package adapterdesignpattern;

 

public class StripeAdapter implements PaymentGateway {

 

               private Stripe stripe = new Stripe();

 

               @Override

               public void pay(double amount) {

                              stripe.makePayment((int) amount);

 

               }

 

}

 

 

package adapterdesignpattern;

 

public class RozarPayAdapter implements PaymentGateway {

 

              

               private RozarPay rozarPay=new RozarPay();

               @Override

               public void pay(double amount) {

                              rozarPay.doTransaction(String.format("%.2f", amount));

                             

               }

 

}

 

 

4. Client

 

 

package adapterdesignpattern;

 

public class PaymentGatewayDemo {

 

               public static void main(String[] args) {

 

                              PaymentGateway payPalAdapter = new PayPalAdapter();

                              payPalAdapter.pay(500.0);

 

                              PaymentGateway stripeAdapter = new StripeAdapter();

                              stripeAdapter.pay(800.0);

 

                              PaymentGateway rozarPayAdapter = new RozarPayAdapter();

                              rozarPayAdapter.pay(900.0);

 

               }

 

}

 

O/p:

Paid $500.0 via PayPal.

Paid $800 via Stripe.

Paid $900.00 via RozarPay.

 

 

Where Adapter Pattern is Used in Java

1. Java I/O (Input/Output) API

  • Example: InputStreamReader
    • Use: Adapts a ByteStream (InputStream) into a CharacterStream (Reader).
    • Pattern: Adapter

Reader reader = new InputStreamReader(new FileInputStream("file.txt"));

    • How it works: InputStreamReader adapts the InputStream to be usable as a Reader.

2. Java Collections Framework

  • Example: Arrays.asList(T... a)
    • Use: Converts (adapts) an array to a List.

List<String> list = Arrays.asList("a", "b", "c");

    • How it works: Wraps the array and exposes it as a List.

3. Java AWT/Swing GUI Event Listeners

  • Example: MouseAdapter, KeyAdapter, etc.
    • Use: Adapter classes provide default (empty) implementations so developers can override only needed methods.

frame.addMouseListener(new MouseAdapter() {

    public void mouseClicked(MouseEvent e) {

        System.out.println("Mouse clicked");

    }

});

    • How it works: MouseAdapter adapts the complex MouseListener interface by providing empty methods.

4. JDBC (Java Database Connectivity)

  • Example: Wrapping custom ResultSet or Connection implementations
    • Use: JDBC drivers adapt vendor-specific DB operations to the standard java.sql.Connection, ResultSet, etc.
    • How it works: Adapters ensure all drivers follow the standard JDBC interface, despite underlying DB differences.

5. Third-party Library Integration

  • Use: Adapting a third-party library's class/interface to your application’s expected interface.
    • Example: Suppose a library gives you a LoggerX class, but your code expects a Logger interface — you can create an adapter.

6. Legacy Code Integration

  • Use: When working with older systems whose classes can’t be modified, you write an adapter to make them compatible with your new system.

 

No comments:

Post a Comment