♯ C# EVOCATION DIFFICULTY: Adept Behavioral

The Command Scroll

Encapsulating a request as an object, allowing parameterization of clients with queues.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Command
ARCANUM SCHOOL: Enchantment // Stored Intent
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CSHARP PATTERN //
using System;
using System.Collections.Generic;

namespace EnterpriseEvocation
{
    public interface ISpellCommand
    {
        void Execute();
        void Undo();
    }

    public class TeleportCommand : ISpellCommand
    {
        private string _previousLocation = "Home";
        public void Execute() => Console.WriteLine("Teleporting to Astral Plane.");
        public void Undo() => Console.WriteLine($"Reverting teleport to {_previousLocation}.");
    }

    public class SpellInvoker
    {
        private readonly Stack<ISpellCommand> _history = new();

        public void Cast(ISpellCommand command)
        {
            command.Execute();
            _history.Push(command);
        }

        public void RewindTime()
        {
            if (_history.Count > 0)
            {
                var cmd = _history.Pop();
                cmd.Undo();
            }
        }
    }
}

The Command pattern captures an invocation into a concrete scroll. This stored intent allows for delayed execution, robust logging, and even the chronomantic ability to step backward through time by undoing applied magic.

Runic Tags: #behavioral #command #undo #csharp