🔴 SCALA ALCHEMY DIFFICULTY: Adept Structural

The Flyweight Particles

Share intrinsic magically dense data across millions of spectral constructs to save memory.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Conjuration // Swarming
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SCALA PATTERN //
// Intrinsic State
case class SpectralCore(color: String, baseDamage: Int)

// Flyweight Factory
object CoreSynthesizer {
  private var cache: Map[String, SpectralCore] = Map()

  def getCore(color: String): SpectralCore = {
    cache.getOrElse(color, {
      val newCore = SpectralCore(color, if (color == "Red") 10 else 5)
      cache += (color -> newCore)
      newCore
    })
  }
}

// Extrinsic State
case class Wisp(x: Double, y: Double, core: SpectralCore) {
  def render(): Unit = println(s"Wisp at $x, $y glowing ${core.color}")
}

The Flyweight prevents arcane memory leaks by caching and sharing the heavy intrinsic cores of identical summoned entities.

Runic Tags: #scala #structural #optimization