Design Pattern
🎯 What Are Design Patterns?
Design patterns are proven solutions to common software design problems. They represent best practices refined over time by experienced developers.
They help you:
Write reusable and maintainable code.
Improve communication between team members using a common vocabulary (e.g., "This uses the Singleton pattern").
Solve structural or behavioral challenges in object-oriented design.
🧩 Categories of Design Patterns
Creational – deal with object creation (e.g., Singleton, Factory).
Structural – deal with class/object composition (e.g., Adapter, Decorator).
Behavioral – deal with object communication (e.g., Observer, Strategy).
🟨 1. Singleton Design Pattern (Creational)
✅ Purpose:
Ensure only one instance of a class exists in the application and provide a global point of access to it.
🔧 When to Use:
🛠 Implementation in Java:
✅ Key Points:
Only one object ever created.
Constructor is
private.Instance is returned via a static method.
🟩 2. Adapter Design Pattern (Structural)
✅ Purpose:
Convert the interface of a class into another interface that clients expect. It allows classes with incompatible interfaces to work together.
🔧 When to Use:
You want to use a class, but its interface is different from what your code expects.
You want to reuse legacy code.
🧱 Example Scenario:
Suppose your client code expects USBCharger, but you have a class OldCharger with a different interface.
🔁 Without Adapter:
🧩 Adapter Code:
🧪 Client Code:
✅ Key Points:
Adapter implements the expected interface.
It translates calls to the actual/legacy interface.
✨ Summary
| Pattern | Category | Intent |
|---|---|---|
| Singleton | Creational | Ensure only one instance of a class |
| Adapter | Structural | Bridge between incompatible interfaces |