DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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

Emmanouil Gkatziouras user avatar by
Emmanouil Gkatziouras
CORE ·
Jul. 25, 18 · Tutorial
Like (7)
Save
Tweet
Share
13.43K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Adapter pattern Design

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using the PostgreSQL Pager With MariaDB Xpand
  • Core Machine Learning Metrics
  • How To Create and Edit Excel XLSX Documents in Java
  • Continuous Development: Building the Thing Right, to Build the Right Thing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: