♯ C# EVOCATION DIFFICULTY: Apprentice Behavioral

The Strategy Grimoire

Defining a family of algorithms, encapsulating each one, and making them interchangeable.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Strategy
ARCANUM SCHOOL: Evocation // Tactical Casting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CSHARP PATTERN //
using System;

namespace EnterpriseEvocation
{
    public interface ICombatStrategy
    {
        void Execute();
    }

    public class AggressiveStrategy : ICombatStrategy
    {
        public void Execute() => Console.WriteLine("Spamming highly destructive AoE fire magic!");
    }

    public class DefensiveStrategy : ICombatStrategy
    {
        public void Execute() => Console.WriteLine("Raising absolute zero ice barriers.");
    }

    public class Battlemage
    {
        private ICombatStrategy _strategy;

        public Battlemage(ICombatStrategy strategy)
        {
            _strategy = strategy;
        }

        public void SetTactics(ICombatStrategy strategy)
        {
            _strategy = strategy;
            Console.WriteLine("Swapping tactical loadout.");
        }

        public void Engage() => _strategy.Execute();
    }
}

A prepared Battlemage carries multiple combat strategies in their grimoire. Depending on the ebb and flow of enterprise battle, they swap their ICombatStrategy interface at runtime.

Runic Tags: #behavioral #strategy #composition #csharp