Explore Topics
+ provide110V() : int
Adapter is a **structural** GoF pattern: *"Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces."* The Client speaks one language (the **Target** interface). The thing you actually want to call speaks another (the **Adaptee**). Neither side can be changed β the Client is shared infra, the Adaptee is legacy or third-party. So you write a thin **Adapter** that IS-A Target (so the Client accepts it) and either HAS-A or IS-A Adaptee (so it can forward and translate). The pattern is sometimes simply called **Wrapper** β but every wrapper that translates a different interface is an Adapter.
- Two interfaces, one bridge β Adapter exists to **reconcile** an interface mismatch, not to add behavior (that's Decorator) and not to control access (that's Proxy).
- You touch neither side β the Client and the Adaptee are both untouchable; only the Adapter is yours to write.
- Two variants: **Class Adapter** (extends Adaptee + implements Target β inheritance) and **Object Adapter** (implements Target + HAS-A Adaptee β composition). They have the same intent but different consequences.
- The hallmark phrase is **code reuse for incompatible legacy** β wrap an already-tested class instead of forking it.
- β¦Lets you reuse an existing, already-tested Adaptee class without modifying it.
- β¦The Client code never knows the Adaptee exists β it sees only the clean Target interface.
- β¦Adding a new incompatible source is purely additive: write a new Adapter, the Client doesn't change (Open/Closed Principle).
- β³Every call goes through one extra hop (the forward) β usually free, but visible in tight loops.
- β³It is sometimes tempting to ship many adapters when you could have just fixed the API β Adapter is a retrofit pattern, not a forward-looking design.
- β³The Adapter is one more class to read, name, and maintain β small but cumulative complexity.
Quiz
Diagrams
extends Adaptee (inheriting its legacy method for free) and implements Target (so the Client accepts it). In Java this works only because Target is an interface β Java's single class inheritance forbids extends Adaptee extends Target at the same time. The Adapter is BOTH an Adaptee and a Target, which is sometimes useful and sometimes a leaky abstraction.implements Target (so the Client accepts it) and holds a private Adaptee field that it forwards calls to. No Java inheritance restriction; the Adaptee can be swapped at runtime by passing a different instance into the constructor; subclasses of the Adaptee can also be adapted polymorphically. This is the variant you should default to in modern Java β composition over inheritance applies here cleanly.A_Adapter holds an A_Adaptee whose interface is different from the Target β the whole point of the pattern is to bridge that gap. In Decorator (right), the wrapper D_Decorator holds a D_Component whose interface is the same as the wrapper's own interface (both implement D_Component). This single difference cascades into every other contrast you have ever heard: Adapter is a retrofit applied once at a boundary; Decorator stacks arbitrarily deep because each layer matches the layer above. Adapter translates an interface; Decorator adds behavior while preserving the interface. If your wrapper has the same interface as what it wraps, you have written a Decorator; if it has a different interface, you have written an Adapter.