The Flyweight
Uses sharing to support large numbers of fine-grained magical entities efficiently.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Conjuration // Optimization
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// TCL PATTERN //
oo::class create RuneParticle {
variable intrinsicColor
constructor {color} { set intrinsicColor $color }
method display {x y} {
puts "Rendering $intrinsicColor rune at $x, $y"
}
}
oo::class create RuneFactory {
variable cache
constructor {} { set cache [dict create] }
method getRune {color} {
if {![dict exists $cache $color]} {
dict set cache $color [RuneParticle new $color]
}
return [dict get $cache $color]
}
}
set factory [RuneFactory new]
set r1 [$factory getRune "NeonPink"]
set r2 [$factory getRune "NeonPink"]
$r1 display 10 20
$r2 display 50 80
puts "Are they the same object? [expr {$r1 eq $r2}]"
The Flyweight
When unleashing a swarm of ten thousand nano-runes, instantiating each individually will shatter the JVM—or in this realm, the Tcl interpreter’s memory limit. The Flyweight binds the intrinsic, unchanging properties to a shared essence, forcing the swarm to share memory. It is efficient, brutal, and elegant.
Runic Tags:
#structural
#flyweight
#optimization
#memory