Tuesday, 29 October 2013

Adapter Design pattern - keypoints

🚀 Enjoying Java Tutorials?

Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

🔔 Subscribe to Ram N Java

Introduction

The Adapter Design Pattern is one of the most frequently asked structural design patterns in technical interviews. It allows two incompatible interfaces to collaborate seamlessly without modifying existing source code. Below is a curated, comprehensive summary of the essential concepts, implementation variations, and interview takeaways you need to know.

1. Intent & Core Purpose

  • Structural Pattern: Adapter is categorized under Gang of Four (GoF) structural design patterns because it concerns class and object composition.
  • Bridge Incompatibility: It converts the interface of a class into another interface that clients expect, letting classes work together that couldn't otherwise due to incompatible interfaces.
  • Also Known As: Wrapper Pattern, because the adapter wraps the existing class (adaptee) inside a compatible layer.

2. The 4 Essential Participants

Participant Role in Architecture
Client Contains the business logic and interacts only via the Target interface.
Target Interface Defines the domain-specific contract and methods that the Client expects to invoke.
Adaptee Existing class or third-party library offering the required functionality under an incompatible interface.
Adapter The intermediate class that implements Target and translates requests to Adaptee method calls.

3. Object Adapter vs. Class Adapter

  • Object Adapter (Composition):
    • Uses object composition by maintaining an instance variable reference to the Adaptee.
    • Implements the Target interface and delegates calls to the composed Adaptee instance.
    • Advantage: Can adapt an Adaptee and any of its subclasses simultaneously. Favors the OOP principle: favor composition over inheritance.
  • Class Adapter (Inheritance):
    • Uses multiple inheritance (in C++) or extends the Adaptee class while implementing the Target interface (in Java).
    • Directly calls inherited superclass methods without creating a separate instance.
    • Trade-off: Commits to a concrete Adaptee class, making it less flexible when working with Adaptee subclasses.

4. When Should You Use It?

  • When you want to reuse an existing class whose interface does not match the one you need.
  • When integrating third-party libraries, legacy components, or APIs whose source code cannot be modified.
  • When building reusable modules that need to coordinate with unrelated or unforeseen classes that don't share compatible interfaces.

5. Real Examples in Java Standard Library

  • java.util.Arrays#asList(): Adapts an array into a standard List interface.
  • java.io.InputStreamReader(InputStream): Adapts a byte stream (InputStream) into a character stream (Reader).
  • java.io.OutputStreamWriter(OutputStream): Adapts a byte stream (OutputStream) into a character stream (Writer).
  • java.util.Collections#enumeration() & list(): Adapts between legacy Enumeration and modern Iterator / Collection interfaces.

Related Tutorials from Ram N Java


Click here to watch in Youtube : https://www.youtube.com/watch?v=mGJRT0TWO5k

Click the below Image to Enlarge


No comments:

Post a Comment

Tutorials