🎸 GROOVY HEX DIFFICULTY: Adept Structural

The Flyweight Hex

Using sharing to support large numbers of fine-grained objects efficiently.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Transmutation // Optimization
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// GROOVY PATTERN //
class Rune {
    final String symbol
    Rune(String s) { 
        this.symbol = s
        println "Forging new rune: $s"
    }
    void glow(String color) { println "Rune $symbol glowing $color" }
}

class RuneFactory {
    private static Map<String, Rune> cache = [:]

    static Rune getRune(String s) {
        cache.computeIfAbsent(s) { new Rune(it) }
    }
}

def r1 = RuneFactory.getRune("ALPHA")
def r2 = RuneFactory.getRune("ALPHA")

r1.glow("Red")
r2.glow("Blue")

assert r1.is(r2) // They are the exact same instance in memory

The Flyweight Hex

Memory limits are real, even in the infinite grid of the JVM. When a system requires millions of identical runic instances, the Flyweight hex intercepts the instantiation process and serves a shared, immutable reference from a centralized cache. Groovy’s computeIfAbsent map method makes this pattern trivial to implement, drastically reducing heap consumption.

Runic Tags: #groovy #structural #flyweight #memoization