🎸 GROOVY HEX DIFFICULTY: Archmage Behavioral

The Visitor Hex

Adding operations to object structures without altering the objects, via Dynamic Dispatch.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Visitor
ARCANUM SCHOOL: Enchantment // Polymorphism
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// GROOVY PATTERN //
class FileSystemNode {}
class Directory extends FileSystemNode { String name }
class TextFile extends FileSystemNode { String content }

class ScannerVisitor {
    void visit(Directory dir) { println "Scanning Directory: ${dir.name}" }
    void visit(TextFile file) { println "Scanning File size: ${file.content.length()}" }
    void visit(Object o) { println "Unknown node" }
}

// Groovy's dynamic dispatch natively supports Double Dispatch
def nodes = [new Directory(name: "/root"), new TextFile(content: "secrets")]
def scanner = new ScannerVisitor()

nodes.each { node ->
    scanner.visit(node) // In Java, this would fail without accept() methods
}

The Visitor Hex

In rigid, static realms, the Visitor pattern requires a chaotic mess of accept(Visitor v) methods to achieve double-dispatch. However, Groovy is a dynamic nexus. Methods are resolved at runtime based on the actual type of the object. A cyber-mage can simply iterate over a heterogeneous list and call visit(node), and Groovy’s meta-object protocol will automatically route the spell to the perfectly matching overloaded method. This utterly eliminates the traditional Visitor boilerplate.

Runic Tags: #groovy #behavioral #visitor #dynamic-dispatch