🐪 OCAML METAMAGIC DIFFICULTY: Adept Creational

The Abstract Factory

A grimoire for forging related elemental artifacts through functorial alchemy.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Abstract Factory
ARCANUM SCHOOL: Conjuration // Functor Alchemy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// OCAML PATTERN //
module type ELEMENT = sig
  type t
  val manifest : unit -> t
  val invoke : t -> string
end

module type FACTORY = sig
  module Fire : ELEMENT
  module Ice : ELEMENT
end

module ElementalFactory : FACTORY = struct
  module Fire = struct
    type t = Flame
    let manifest () = Flame
    let invoke Flame = "Searing Heat!"
  end

  module Ice = struct
    type t = Frost
    let manifest () = Frost
    let invoke Frost = "Biting Cold!"
  end
end

Through the alchemy of OCaml modules, the Abstract Factory pattern becomes an exercise in summoning related elements bound by a common grimoire.

Runic Tags: #Caml Metamagic #Modules #Functors #OCaml