34 lines
1.0 KiB
Markdown
34 lines
1.0 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 11+.
|
|
|
|
## Files
|
|
|
|
| File | Role |
|
|
|---|---|
|
|
| `PaymentGateway.java` | Target interface (what your app expects) |
|
|
| `StripeClient.java` | Adaptee (third-party SDK you can't modify) |
|
|
| `StripePaymentAdapter.java` | Adapter (bridges the two) |
|
|
| `OrderService.java` | Client (only uses PaymentGateway) |
|
|
| `Main.java` | Demo entry point |
|
|
|
|
## See Also
|
|
|
|
- Full article: https://ankurm.com/adapter-design-pattern-java/
|
|
- All design patterns: https://ankurm.com/design-patterns-java/
|