37 lines
1.2 KiB
Markdown
37 lines
1.2 KiB
Markdown
# Adapter Design Pattern — Java Example
|
|
|
|
**Pattern:** Structural → Adapter
|
|
**Article:** https://ankurm.com/adapter-design-pattern-java/
|
|
|
|
## What this example shows
|
|
|
|
Wraps a third-party `StripeClient` (with its own API) behind a `PaymentGateway` interface that your application code expects. The `OrderService` client never touches `StripeClient` directly — it only sees `PaymentGateway`. Swapping payment providers requires changing one line.
|
|
|
|
## How to run
|
|
|
|
```bash
|
|
# From this folder:
|
|
javac adapter/*.java
|
|
java adapter.Main
|
|
```
|
|
|
|
Requires Java 25.
|
|
|
|
## Files
|
|
|
|
| Post Section | File(s) |
|
|
|---|---|
|
|
| Step 1 — Define the Target Interface | `PaymentGateway.java` |
|
|
| Step 2 — The Adaptee (What You're Wrapping) | `StripeClient.java` |
|
|
| Step 3 — The Object Adapter | `StripePaymentAdapter.java` |
|
|
| Step 4 — The Client (Knows Nothing About Stripe) | `OrderService.java` |
|
|
| Putting It Together: The Main Demo | `Main.java` |
|
|
| Object Adapter vs Class Adapter | `StripePaymentClassAdapter.java` |
|
|
|
|
Note: the `InputStreamReader` JDK-adapter snippet is illustrative only — it is not part of this repository's runnable example.
|
|
|
|
## See Also
|
|
|
|
- Full article: https://ankurm.com/adapter-design-pattern-java/
|
|
- All design patterns: https://ankurm.com/design-patterns-java/
|