Patterns are like Tools
A good engineer knows when to use a hammer and when to use a screwdriver. Use this practice list to develop the intuition for selecting the right "Tool" for the job.
Pattern Matching Challenges
| Problem Scenario | Correct Pattern |
|---|---|
| A system needs to support multiple payment methods (Paypal, Credit Card, UPI). | Strategy |
| You need to notify multiple services (Email, SMS, Slack) when an order is placed. | Observer |
| An app needs to create different types of documents (PDF, Word, TXT) based on user input. | Factory |
| You want to add "extra toppings" or "optional features" to an existing object. | Decorator |
| You need to ensure only one instance of a Database Connection exists. | Singleton |
| You need to access a complex subsystem through a simple, unified interface. | Facade |
| You want to convert an incompatible interface to a compatible one (Legacy code). | Adapter |
| You need to process a request through a series of filters/handlers. | Chain of Responsibility |
| You want to capture and externalize an object's internal state to restore it later. | Memento |
| You want to decouple an abstraction from its implementation so they can vary independently. | Bridge |
Interview Reflector
For each pattern you implement:
- Does it follow SRP?
- Does it follow OCP?
- Is it easy for another developer to understand?
Final Takeaway
Don't memorize definitions. Memorize Scenarios. If the interviewer says "Multiple ways to do X," you say "Strategy." If they say "One to many updates," you say "Observer." 埋