Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Understanding the Adapter Design Pattern

Tech Sep 26 4

Definition

The Adapter Patttern, also known as the Wrapper Pattern, converts the interface of a class into another interface that clients expect. This allows classes with incompatible interfaces to work together without modifying their existing code.

When an existing class's interface does not match your requirements, you can create a new class that implements the desired interface while wrapping the existing class. This approach adheres to the Open-Closed Principle.

The pattern involves three core roles:

  • Target Interface: The expected interface for the current system, which can be an abstract class or interface. Clients interact with this interface—analogous to a power outlet.

  • Adaptee Class: The existing component with an incompatible interface. This can be a class or interface that needs adaptation—analogous to a plug.

  • Adapter Class: A translator that connects the Target interface and Adaptee. By containing or inheriting from the Adaptee, it transforms the Adaptee's interface into the Target interface—analogous to a travel power converter.

Characteristics

Advantages:

  • Clients can transparently call Target methods through the Adapter.
  • Existing classes are reused without modification to their source code.
  • The pattern decouples Target and Adaptee classes, resolving interface incompatibility.
  • It supports the Open-Closed Principle in many business scenarios.

Disadvantages:

  • Writing adapters requires comprehensive consideration of business context, potentially increasing system complexity.
  • Excessive use of adapters can reduce code readability and make the system structure appear chaotic.

Implementation

Object Adapter Pattern

The object adapter pattern uses composition to wrap an Adaptee instance with in the Adapter class.

public interface NotificationService {
    void send(String message);
}

public class EmailSender {
    public void emailNotify(String content) {
        System.out.println("Email sent: " + content);
    }
}

public class SmsGateway {
    public void smsDeliver(String text) {
        System.out.println("SMS delivered: " + text);
    }
}

public class EmailNotificationAdapter implements NotificationService {
    private EmailSender emailSender;

    public EmailNotificationAdapter(EmailSender emailSender) {
        this.emailSender = emailSender;
    }

    @Override
    public void send(String message) {
        emailSender.emailNotify("Notification - " + message);
    }
}

public class SmsNotificationAdapter implements NotificationService {
    private SmsGateway smsGateway;

    public SmsNotificationAdapter(SmsGateway smsGateway) {
        this.smsGateway = smsGateway;
    }

    @Override
    public void send(String message) {
        smsGateway.smsDeliver("Notification - " + message);
    }
}

public class Application {
    public static void main(String[] args) {
        System.out.println("Object Adapter Pattern Test:");
        
        NotificationService emailService = new EmailNotificationAdapter(new EmailSender());
        emailService.send("System alert");

        NotificationService smsService = new SmsNotificationAdapter(new SmsGateway());
        smsService.send("System alert");
    }
}

Output:

Object Adapter Pattern Test:
Email sent: Notification - System alert
SMS delivered: Notification - System alert

Class Adapter Pattern

The class adapter pattern uses inheritance to inherit from the Adaptee while implementing the Target interface. Java does not support multiple inheritance, so the Adapter extends the Adaptee and implements the Target.

public interface DataSource {
    void output();
}

public class LegacyDatabase {
    public void legacyRead() {
        System.out.println("Reading data from legacy system");
    }
}

public class DataAccessAdapter extends LegacyDatabase implements DataSource {
    @Override
    public void output() {
        legacyRead();
    }
}

public class Client {
    public static void main(String[] args) {
        System.out.println("Class Adapter Pattern Test:");
        DataSource dataSource = new DataAccessAdapter();
        dataSource.output();
    }
}

Output:

Class Adapter Pattern Test:
Reading data from legacy system

Comparison

The class adapter pattern creates a tighter coupling between Target and Adaptee, requiring developers to understand the internal structure of existing components. Due to these constraints, the object adapter pattern is more commonly used in practical applications, as it promotes loose coupling and greater flexibility.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.