Skip to main content

Overview

Category

Behavioral Design Pattern

Purpose

Encapsulate interchangeable algorithms

Problem Statement

The Challenge

The chatbot needs to determine user intent and fetch appropriate context. We needed:
  1. Multiple classification algorithms: Keyword matching vs LLM analysis
  2. Multiple context sources: Dining, announcements, or general
  3. Runtime flexibility: Switch via configuration without code changes
Without Strategy Pattern, we would need complex if-elif chains that violate the Open-Closed Principle.

Solution 1: Intent Classification


Solution 2: Context Fetching

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 configuration

Testability

Mock strategies easily in unit tests