The Chain of Responsibility Hex
Passing requests through a gauntlet of warding filters.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Abjuration // Warding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// KOTLIN PATTERN //
data class MagicalRequest(val power: Int, var handled: Boolean = false)
typealias SpellHandler = (MagicalRequest) -> MagicalRequest
val apprenticeWard: SpellHandler = { req ->
if (req.power < 10) req.copy(handled = true).also { println("Apprentice absorbed it.") }
else req
}
val archmageWard: SpellHandler = { req ->
if (!req.handled && req.power >= 10) req.copy(handled = true).also { println("Archmage deflected it.") }
else req
}
val chain = listOf(apprenticeWard, archmageWard)
fun process(req: MagicalRequest) = chain.fold(req) { r, handler -> handler(r) }
The Chain of Responsibility Hex
When a magical attack hits the system, it must be filtered through multiple layers of defense. The Chain of Responsibility routes the payload sequentially. In functional Kotlin, we can map handlers as pure functions and fold them over the request, creating an elegant, immutable chain of wards.
Runic Tags:
#kotlin
#behavioral
#chain-of-responsibility