Adapter Pattern Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Adapter Pattern runs in O(1) time and O(1) space, in Java as in every other language.
- ✓Java is where this pattern was written down and where it is most visible in the standard library.
- ✓Prefer composition, hold the adaptee in a field, over extending it, since Java allows only one superclass and inheriting for reuse couples you to the adaptee's entire surface.
- ✓The Java trap: Arrays.asList is the trap the standard library sets: it returns a FIXED-SIZE view backed by the original array, so add and remove throw UnsupportedOperationException, and writing through the list mutates the array behind it.
- ✓An adapter adds one virtual call per operation, which the JIT will inline once the call site is monomorphic, the common case, making it effectively free.
What Adapter Pattern actually is
Adapter Pattern in Java: what you actually reach for
Java implementation
import java.io.*;
import java.util.*;
// The target the application wants.
public interface Notifier {
void send(String message);
}
// Third-party, unmodifiable.
final class LegacySmsClient {
void transmit(String body, int retries) { /* ... */ }
}
// Object adapter: COMPOSITION, not inheritance.
public final class SmsNotifier implements Notifier {
private final LegacySmsClient client;
public SmsNotifier(LegacySmsClient client) {
this.client = client;
}
@Override
public void send(String message) {
client.transmit(message, 3);
}
}
class Usage {
void examples() throws IOException {
// The JDK's own adapters:
InputStream bytes = new FileInputStream("data.txt");
Reader chars = new InputStreamReader(bytes, "UTF-8"); // bytes -> chars
String[] array = {"a", "b"};
List<String> view = Arrays.asList(array); // FIXED-SIZE view
view.set(0, "z"); // also mutates the array
// view.add("c"); // UnsupportedOperationException
// A one-method target needs no class at all:
Notifier printer = System.out::println;
printer.send("hello");
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for Adapter Pattern?
Java is where this pattern was written down and where it is most visible in the standard library. InputStreamReader adapts a byte-oriented InputStream to a character-oriented Reader; Arrays.asList adapts an array to a List; Collections.enumeration adapts an Iterator to the legacy Enumeration. Each is a textbook object adapter you have used without naming it.
What is the most common Java mistake when implementing Adapter Pattern?
Arrays.asList is the trap the standard library sets: it returns a FIXED-SIZE view backed by the original array, so add and remove throw UnsupportedOperationException, and writing through the list mutates the array behind it. Worse, Arrays.asList(intArray) on a primitive array produces a single-element List<int[]> rather than a list of integers, because autoboxing does not apply to the array itself, it compiles cleanly and is wrong.
How does Adapter Pattern actually perform in Java?
An adapter adds one virtual call per operation, which the JIT will inline once the call site is monomorphic, the common case, making it effectively free. It stops being free when several adapter implementations reach the same call site: the site becomes megamorphic, inlining stops, and every call goes through the vtable. Wrapping a stream in several adapter layers also adds a copy per layer unless each is buffered. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Devin free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.