The Flyweight of Web Runes
Efficiently share large volumes of arcane glyphs to save web memory.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Transmutation // Aether Optimization
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// PURESCRIPT PATTERN //
module Arcane.Flyweight where
import Prelude
import Data.Map as Map
import Data.Maybe (fromMaybe)
-- Intrinsic state (Flyweight)
type RuneGlyph = { symbol :: String, magicalCost :: Int }
-- Extrinsic state
type PlacedRune = { glyph :: RuneGlyph, x :: Int, y :: Int }
-- Flyweight Factory
type GrimoireCache = Map.Map String RuneGlyph
getRune :: String -> GrimoireCache -> RuneGlyph
getRune key cache =
fromMaybe { symbol: "?", magicalCost: 0 } (Map.lookup key cache)
placeRune :: RuneGlyph -> Int -> Int -> PlacedRune
placeRune glyph x y = { glyph, x, y }
Runic Tags:
#structural
#flyweight
#purescript
#runes