Fundamentals 15 min read

Adapter Design Pattern: Concepts, Structure, Code Samples, and Spring MVC Application

This article explains the Adapter design pattern, illustrates its real‑world analogies, describes object and class adapter variants, provides complete Java code examples, demonstrates a voltage‑conversion demo, and shows how Spring MVC employs adapters to decouple controllers from request handling.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Adapter Design Pattern: Concepts, Structure, Code Samples, and Spring MVC Application

Problem

When building a stock‑monitoring system the data provider supplies XML, but later third‑party services only expose JSON. Changing the existing code is undesirable, so an adapter is needed to translate between the two formats.

Real‑World Analogy

An adapter is like a laptop power brick, a universal charger, or a multi‑port cable – it converts one interface to another without the client noticing.

Basic Introduction

The Adapter pattern converts the interface of one class into another interface that clients expect, allowing incompatible classes to work together. It is also known as a Wrapper.

It is a structural design pattern.

Two main types: Class Adapter and Object Adapter.

Working Principle

The adapter hides the conversion logic from the client.

From the client’s perspective the adapter and the adaptee are decoupled.

The client calls the target interface; the adapter forwards the call to the adaptee.

The client receives results as if it interacted directly with the target.

Object Adapter Structure

The object adapter implements the target interface and holds a reference to the adaptee.

Client (Target user) – contains business logic.

Target interface – defines the methods the client expects.

Adaptee (Service) – existing class with an incompatible interface.

Adapter – implements Target, delegates calls to the Adaptee.

Coding

public interface Target { /* client request method */ void request(); }
public class Adaptee { /* original method */ public void specificRequest() { /* business code */ } }
public class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void request() { adaptee.specificRequest(); } }
public class Client { public static void main(String[] args) { Adaptee adaptee = new Adaptee(); Target target = new Adapter(adaptee); target.request(); } }

Class Adapter

Uses inheritance to combine the client and service interfaces. Java cannot inherit multiple classes, so a workaround is to let the adapter extend the Adaptee and implement the Target interface.

public class Adaptee { public void adapteeRequest() { System.out.println("Adaptee method"); } }
public interface Target { void request(); }
public class Adapter extends Adaptee implements Target { @Override public void request() { // ... additional logic ... super.adapteeRequest(); // ... additional logic ... } }
public class Client { public static void main(String[] args) { Target adapterTarget = new Adapter(); adapterTarget.request(); } }

Suitable Scenarios

When a class’s interface is incompatible with existing code.

When you need a middle layer to connect legacy or third‑party components with a new system.

When multiple subclasses share common behavior that cannot be added to each subclass without code duplication.

Demo: Voltage Adapter

A 220 V AC source (Adaptee) is adapted to a 5 V DC target for a phone charger.

public class Volatage220V { public final int output = 220; public int output220v() { System.out.println("Output voltage " + output); return output; } }
public interface IVoltage5V { int output5V(); }
public class Phone { public void charging(IVoltage5V v) { if (v.output5V() == 5) { System.out.println("Voltage 5V, start charging"); } else { System.out.println("Voltage not suitable, cannot charge"); } } }
public class VoltageAdapter implements IVoltage5V { private Volatage220V voltage220V; public VoltageAdapter(Volatage220V v) { this.voltage220V = v; } @Override public int output5V() { int dst = 0; if (voltage220V != null) { int src = voltage220V.output220v(); System.out.println("Adapter working"); dst = src / 44; System.out.println("Adapter finished, output voltage " + dst); } return dst; } }
public class Client { public static void main(String[] args) { Phone phone = new Phone(); phone.charging(new VoltageAdapter(new Volatage220V())); } }

Advantages & Disadvantages

Promotes Single Responsibility by separating conversion logic.

Supports Open/Closed Principle – new adapters can be added without modifying client code.

Increases overall complexity due to additional interfaces and classes.

Adapter in Spring MVC

Spring uses the term HandlerAdapter to adapt various controller types to the DispatcherServlet . The servlet maintains a list of HandlerAdapter instances, selects the appropriate one at runtime, and invokes its handle method.

public class DispatcherServlet extends FrameworkServlet { private List
handlerAdapters; private void initHandlerAdapters(ApplicationContext context) { // ... populate handlerAdapters ... }
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException { for (HandlerAdapter adapter : this.handlerAdapters) { if (adapter.supports(handler)) { return adapter; } } }

The HandlerAdapter interface defines:

public interface HandlerAdapter { boolean supports(Object handler); ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; long getLastModified(HttpServletRequest request, Object handler); }

During request dispatch, the servlet obtains the appropriate adapter and delegates the request handling, eliminating large if‑else blocks and making the framework easily extensible for new controller types.

References

"Design Patterns: Elements of Reusable Object‑Oriented Software" (Gang of Four)

"Head First Design Patterns"

https://refactoringguru.cn/design-patterns/adapter

https://blog.csdn.net/lu__peng/article/details/79117894

https://juejin.im/post/5ba28986f265da0abc2b6084#heading-12

design patternsJavasoftware architectureSpring MVCAdapter PatternClass AdapterObject Adapter
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.