The Factory Method Ritual
Delegate the instantiation of specific spells to subclass covens.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Factory Method
ARCANUM SCHOOL: Conjuration // Subclass Delegation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CSHARP PATTERN //
using System;
namespace EnterpriseEvocation
{
public interface ISpell
{
void Cast();
}
public class Fireball : ISpell
{
public void Cast() => Console.WriteLine("A great sphere of fire bursts forth!");
}
public class IceLance : ISpell
{
public void Cast() => Console.WriteLine("A piercing shard of ice streaks through the air!");
}
public abstract class SpellWeaver
{
public abstract ISpell ForgeSpell();
public void ChannelAndCast()
{
Console.WriteLine("Gathering mana...");
var spell = ForgeSpell();
spell.Cast();
}
}
public class Pyromancer : SpellWeaver
{
public override ISpell ForgeSpell() => new Fireball();
}
public class Cryomancer : SpellWeaver
{
public override ISpell ForgeSpell() => new IceLance();
}
}
By deferring the exact instantiation of a spell to specialized subclass covens, the Factory Method allows high-level channeling logic to remain pure and untainted by the specifics of elemental affinity.
Runic Tags:
#creational
#factory-method
#polymorphism
#csharp