Patterns Overview
This project demonstrates the practical application of three core design patterns from the Gang of Four catalog.Strategy
Behavioral pattern for dynamic algorithm selection
Factory
Creational pattern for OCP-compliant object creation
Singleton
Creational pattern for resource management
Why These Patterns?
Strategy Pattern
Strategy Pattern
Problem: Multiple classification algorithms needed with runtime switchingSolution: Encapsulate each algorithm in a separate class implementing a common interfaceBenefit: Add new classifiers without modifying existing code
Factory Pattern
Factory Pattern
Problem: Complex object creation logic scattered throughout codebaseSolution: Centralize creation in factory functions with dictionary registryBenefit: OCP compliance - extend without modifying
Singleton Pattern
Singleton Pattern
Problem: Expensive database/LLM client initialization on every requestSolution: Single instance with lazy initialization and thread safetyBenefit: Connection pooling, resource efficiency
Pattern Interaction
1
User Sends Message
User sends “bugün menüde ne var?” via WhatsApp
2
Factory Creates Classifier
get_classifier() returns cached HybridClassifier (Singleton-like caching)3
Strategy Classifies Intent
HybridClassifier.classify() returns "dining"4
Factory Creates Strategy
get_context_strategy("dining") returns new DiningStrategy instance5
Strategy Fetches Context
DiningStrategy.fetch() queries MongoDB (Singleton) and formats data6
LLM Generates Response
Gemini generates response with context and sends back to user