The Adapter pattern is a structural pattern that lets objects with incompatible interfaces work together by converting one class's interface into another interface the client expects. It's used to integrate code you can't or don't want to modify with code that expects a different contract.
Wrapping a legacy XmlLogger behind a Logger interface used by the rest of the application lets new code call logger.log(message) while the adapter internally calls xmlLogger.writeXmlEntry(message).
interface Logger {
void log(String message);
}
class LegacyXmlLogger {
void writeXmlEntry(String msg) { System.out.println("<log>" + msg + "</log>"); }
}
class XmlLoggerAdapter implements Logger {
private LegacyXmlLogger legacyLogger;
XmlLoggerAdapter(LegacyXmlLogger legacyLogger) { this.legacyLogger = legacyLogger; }
public void log(String message) { legacyLogger.writeXmlEntry(message); }
}A concise interview answer is
Adapter converts one interface into another that the client already expects, which is exactly what I need when integrating a third-party library or legacy code without rewriting either side. I write a class that implements the interface my code expects and internally delegates to the incompatible class, translating calls between the two.
Last updated: 16 July 2026
Previous question: What are the common pitfalls of using the Prototype pattern?
Next question: How does the Adapter pattern differ from the Decorator pattern?