🎸 GROOVY HEX DIFFICULTY: Archmage Structural

The Decorator Hex

Dynamically attaching additional responsibilities to an object via Traits and MetaClass.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Decorator
ARCANUM SCHOOL: Transmutation // Enhancement
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// GROOVY PATTERN //
interface Artifact { String getDescription() }

class BaseSword implements Artifact {
    String getDescription() { return "Steel Sword" }
}

// Using Groovy Traits for dynamic decoration
trait Flaming {
    String getDescription() { return super.getDescription() + " of Flames" }
}

trait Cursed {
    String getDescription() { return "Cursed " + super.getDescription() }
}

def sword = new BaseSword()
def magicSword = sword.withTraits(Flaming, Cursed)

println magicSword.getDescription()

The Decorator Hex

While ancient traditions require passing objects into wrapper classes to decorate them, Groovy allows an Archmage to attach traits at runtime via withTraits. This profoundly shifts the Decorator pattern from a structural compile-time chore into a dynamic, meta-object enhancement ritual. Apply elemental damage to your swords instantly as they are drawn!

Runic Tags: #groovy #structural #decorator #traits #metaprogramming