🎸 GROOVY HEX DIFFICULTY: Apprentice Behavioral

The Strategy Hex

Defining a family of interchangeable algorithms and behaviors via Closures.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Enchantment // Polymorphism
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// GROOVY PATTERN //
class Encryptor {
    Closure strategy

    String encrypt(String text) {
        strategy(text)
    }
}

def rot13 = { String t -> 
    t.collect { (it as char) + 13 as char }.join() 
}

def reverse = { String t -> t.reverse() }

def cipher = new Encryptor(strategy: reverse)
println cipher.encrypt("SECRET_PAYLOAD")

cipher.strategy = rot13
println cipher.encrypt("SECRET_PAYLOAD")

The Strategy Hex

In the Java dimensional plane, Strategy requires an interface and numerous implementation classes. In Groovy, Strategy is merely a Closure injected at runtime. Algorithms, data processors, and attack vectors can be passed around like data, allowing the core Encryptor class to mutate its core behavior depending on the cyber-mage’s immediate tactical needs.

Runic Tags: #groovy #behavioral #strategy #closures