Overview
Category
Behavioral Design Pattern
Purpose
Encapsulate interchangeable algorithms
Problem Statement
The Challenge
The Challenge
The chatbot needs to determine user intent and fetch appropriate context. We needed:
- Multiple classification algorithms: Keyword matching vs LLM analysis
- Multiple context sources: Dining, announcements, or general
- Runtime flexibility: Switch via configuration without code changes
Without Strategy Pattern, we would need complex if-elif chains that violate the Open-Closed Principle.
Why Strategy Pattern?
Why Strategy Pattern?
Solution 1: Intent Classification
- UML Diagram
- Code Example
- File Locations
Solution 2: Context Fetching
- UML Diagram
- Code Example
Each Strategy owns its formatting logic. The Service layer returns raw
List[Dict], and the Strategy transforms it into an LLM-optimized string.Benefits
Open-Closed Principle
Add new classifiers without modifying existing code
Single Responsibility
Each strategy handles one algorithm
Runtime Flexibility
Switch via
.env configurationTestability
Mock strategies easily in unit tests