Update Design and Architecture

2025-07-08 21:27:22 +02:00
parent 0836d935df
commit d39b9c2be4

@@ -2,30 +2,40 @@
## Motivation
- Provide an application-wide, strongly typed key-value store
- Avoid tight coupling of modules
- Allow subscribing to value changes without risking memory leaks
- Provide an application-wide, strongly typed key-value store
- Avoid tight coupling of modules
- Allow subscribing to value changes without risking memory leaks
- Support multiple independent named contexts for better modularity and isolation
## Why Enum Singleton
- Simple
- Thread-safe by Java specification
- Cannot be duplicated through serialization
- Simple
- Thread-safe by Java specification
- Cannot be duplicated through serialization
## Multi-Context Extension
- `Ctx` manages multiple named contexts identified by strings
- Each named context is an independent key-value store with its own listeners
- Enables modular, isolated state management within the same application
- Backwards compatible: default context operations still available via `Ctx.INSTANCE`
## Why Weak References for Listeners
- Listeners should not prevent GC
- Weak references automatically clean up when listener objects are unreachable
- Listeners should not prevent garbage collection
- Weak references automatically clean up when listener objects are unreachable
## Thread Safety
- Uses `ConcurrentHashMap`
- Listeners stored in `CopyOnWriteArrayList`
- Uses `ConcurrentHashMap` for concurrent access
- Listeners stored in `CopyOnWriteArrayList` for safe iteration
## Trade-offs
- Slightly higher cost for weak-reference listener lookups
- Enum cannot be extended (intentional for safe singleton)
- Slightly higher cost for weak-reference listener lookups
- Enum cannot be extended (intentional for safe singleton)
- Multi-context adds minimal complexity but greatly improves flexibility
---
[[Home]] | [[Programming Guide]] | [[API Reference]] | [[FAQ]] | [[Contributing]]