๐งช๏ธ Configuration Is Scattered
โ Problem Overviewโ
When an application grows, configuration and dependency initialization often become scattered, duplicated, or inconsistent.
Manually wiring values in multiple places leads to tight coupling and reduced flexibility.
Common issues include:
- Initialization logic is duplicated and difficult to manage
- Inconsistent configuration handling across modules
- Hard to switch environments between test/staging/production
โ Solution Overviewโ
Use the Factory
pattern to centralize configuration creation,
and the Singleton
pattern to ensure consistent reuse of that configuration across the system.
Concern | Applied Pattern |
---|---|
Centralized configuration init | Factory |
Shared access to instances | Singleton |
โ Pattern Synergyโ
Role | Example Implementation |
---|---|
Configuration source | ConfigFactory (initializer) |
Shared instance | AppConfig (singleton) |
Consumers | ServiceA , ServiceB |
Factory
creates a single, consistent configuration structureSingleton
ensures shared access and avoids redundant instantiation
โ UML Class Diagramโ
โ Explanationโ
This design separates configuration concerns from usage:
ConfigFactory
encapsulates how configuration is created and loaded (Factory)AppConfig
is created once and shared system-wide (Singleton)- Services depend only on the
AppConfig
interface, not the creation logic
This enables clean initialization and easier testing or replacement.
โ Practical Notesโ
- โ Environment switching (dev/staging/prod) becomes easier
- โ Configuration can be mocked or overridden in test contexts
- โ Also applicable to shared resources like DB connections or auth providers
Examples:
- Wrapping
.env
loading into a singleAppConfig
class - Injecting mock
ConfigFactory
in unit tests - Managing shared configuration for internal libraries and services
โ Summaryโ
- Use
Factory
to centralize configuration creation logic - Use
Singleton
to maintain a consistent, shared config instance - Greatly improves clarity and maintainability of application setup
- Enables safe, flexible environment switching and testability
This design provides a robust foundation for scalable and reliable configuration management.