✨ ARCANUM RUNE DIFFICULTY: Apprentice Creational

The Factory Method of the Progenitor

Delegate the creation of enchanted items to specialized constructor functions.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Factory Method
ARCANUM SCHOOL: Conjuration // Summoning Construct
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
datatype element = Fire | Frost | Arcane

signature SCROLL = sig
  val read : unit -> string
end

fun createScroll Fire = 
    let structure S = struct fun read () = "Burns your hands!" end 
    in S.read end
  | createScroll Frost = 
    let structure S = struct fun read () = "Chills your soul!" end 
    in S.read end
  | createScroll Arcane = 
    let structure S = struct fun read () = "Expands your mind!" end 
    in S.read end
    
(* Functional approach to Factory Method *)
fun invokeScrollFactory elem = createScroll elem

val castFire = invokeScrollFactory Fire ()

The Factory Method in the ML Progenitor is seamlessly supplanted by Standard ML’s algebraic datatypes and pattern matching. A single constructor function matches on the variants of a domain and yields the correct implementation. This pure functional approach ensures exhaustiveness checks by the SML compiler, preventing a careless apprentice from forgetting to handle a new mystical element.

Runic Tags: #constructors #pattern matching