The Proxy Sentinel
Control access to an astronomically expensive artifact until it is strictly necessary to invoke it.
β§ GANG OF FOUR LINEAGE & ARCANUM β§
GOF DESIGN PATTERN: Proxy
ARCANUM SCHOOL: Abjuration // Warding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SCALA PATTERN //
trait Oracle {
def scry(target: String): String
}
class TrueOracle extends Oracle {
// Expensive initialization
Thread.sleep(1000)
override def scry(target: String): String = s"I see $target's doom."
}
class ProxyOracle extends Oracle {
private lazy val trueOracle = new TrueOracle()
override def scry(target: String): String = {
if (target == "Forbidden") "Access Denied by Proxy."
else trueOracle.scry(target)
}
}
Using Scalaβs lazy val, the Proxy defers the creation of the underlying TrueOracle until absolutely required, while also providing a layer of access control.
Runic Tags:
#scala
#structural
#lazy-evaluation
#warding