♯ C# EVOCATION DIFFICULTY: Apprentice Structural

The Decorator Matrix

Dynamically attaching additional responsibilities to an object at runtime.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Decorator
ARCANUM SCHOOL: Enchantment // Dynamic Warding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CSHARP PATTERN //
using System;

namespace EnterpriseEvocation
{
    public interface IArmor
    {
        string GetDescription();
        int GetDefense();
    }

    public class BaseArmor : IArmor
    {
        public string GetDescription() => "Standard Robe";
        public int GetDefense() => 5;
    }

    public abstract class ArmorEnchantment : IArmor
    {
        protected readonly IArmor _baseArmor;

        protected ArmorEnchantment(IArmor armor)
        {
            _baseArmor = armor;
        }

        public virtual string GetDescription() => _baseArmor.GetDescription();
        public virtual int GetDefense() => _baseArmor.GetDefense();
    }

    public class FireResistanceEnchantment : ArmorEnchantment
    {
        public FireResistanceEnchantment(IArmor armor) : base(armor) { }

        public override string GetDescription() => $"{_baseArmor.GetDescription()}, Warded against Flame";
        public override int GetDefense() => _baseArmor.GetDefense() + 10;
    }
}

Instead of forging a completely new item, a skilled Enchanter wraps the target in successive layers of dynamic warding. The Decorator matrix allows multiple modifiers to be stacked seamlessly onto IArmor implementations.

Runic Tags: #structural #decorator #wrapping #csharp