Clone
3
Design and Architecture
Leo Galambos edited this page 2025-07-08 21:27:22 +02:00

Design and Architecture

Motivation

  • 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

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 garbage collection
  • Weak references automatically clean up when listener objects are unreachable

Thread Safety

  • 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)
  • Multi-context adds minimal complexity but greatly improves flexibility

Home | Programming Guide | API Reference | FAQ | Contributing