Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Structural Design Patterns: How to Use the Adapter Pattern

DZone's Guide to

Structural Design Patterns: How to Use the Adapter Pattern

Want to learn more about structural design patterns? Check out this post on using the adapter pattern and

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

In previous blog posts, we focused on creational design patterns such as the builder pattern, the abstract factory/factory pattern, the singleton and the prototype pattern.

We will switch our focus to structural design patterns. By using structural design patterns, our goal is to ease the design by identifying a simple way to realize the relationships between entities.

The adapter pattern is one of the most used and well known structural patterns. Its major usage is for when you want to make classes work with other classes without having to change its source code.

Imagine the scenario of refactoring code. Then, you stumble upon a class sending emails:

package com.gkatzioura.design.structural.adapter;

import java.util.List;

public class MailSender {

    private String title;
    private String message;
    private List<String> recipients;

    public MailSender(String title, String message, List<String> recipients) {
        this.title = title;
        this.message = message;
        this.recipients = recipients;
    }

    public void sendMessage() {

    }

    public void sendHtmlMessage(String htmlTemplate) {

    }

}


The MailSender class has the ability to send an email, but you can also use it to send an email containing HTML.

We want to add some subscribe/notify functionality in our code base so that people could receive a simple message in the form of SMS, email, webhooks, etc.

We have also come up with an interface for the notify functionality:

package com.gkatzioura.design.structural.adapter;

public interface Notifier {

    void notify(String recipients,String message);

}


As you can see, our notify action does not really care about the subject or formatting of the message. You just have to notify the subscribes.

Among the REST implementation, an email implementation should be provided. We can either provide a new email implementation, which implements the notifier interface, or we can change the original email class.

However, there is a third option. You can use the adapter pattern and create a class so that you can reuse your old email class.

By doing this, you do not alter your old email class, and, therefore, the code uses its functionality, and you skip creating a new class, therefore, having to maintain one extra class.

package com.gkatzioura.design.structural.adapter;

import java.util.List;

public class MailNotifierAdapter implements Notifier {

    private static final String NOTIFICATION_TITLE = "System notification";

    public void notify(List<String> recipients, String message) {

        MailSender mailSender = new MailSender(NOTIFICATION_TITLE,message,recipients);
        mailSender.sendMessage();
    }
}


You can find the source code on GitHub.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,tutorial ,string ,adapter pattern ,structural design pattern ,structural design

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}