Singleton of the Nexus Node
Ensure a class only has one instance, and provide a global point of access to it.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Singleton
ARCANUM SCHOOL: Abjuration // Ley-line Tapping
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// RUST IDIOMATIC PATTERN //
use std::sync::{Arc, Mutex};
use lazy_static::lazy_static;
pub struct NexusCore {
mana_level: u32,
}
impl NexusCore {
fn new() -> Self {
Self { mana_level: 100 }
}
pub fn drain(&mut self, amount: u32) {
self.mana_level = self.mana_level.saturating_sub(amount);
}
}
lazy_static! {
pub static ref THE_NEXUS: Arc<Mutex<NexusCore>> = Arc::new(Mutex::new(NexusCore::new()));
}
// Usage:
// let mut nexus = THE_NEXUS.lock().unwrap();
// nexus.drain(10);
There is only one true Nexus Node in the grid, a singularity of pure arcane data. To allow multiple manifestations of the core would tear the fabric of the matrix asunder. The Singleton pattern ensures this cosmological constant.
By utilizing lazy_static or OnceCell, along with Arc and Mutex for thread-safe access across the multiversal threads, the adept guarantees that every spell tapping into the Nexus draws from the exact same reservoir of power.
Runic Tags:
#creational
#singleton
#abjuration
#global-state