♯ C# EVOCATION DIFFICULTY: Archmage Structural

The Flyweight Swarm

Sharing intrinsic state to support huge numbers of fine-grained objects efficiently.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Necromancy // Swarm Optimization
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CSHARP PATTERN //
using System;
using System.Collections.Generic;

namespace EnterpriseEvocation
{
    public class SkeletonModel
    {
        public byte[] MeshData { get; } = new byte[10000]; 
        public string SharedTexture { get; } = "bone_diffuse.png";
    }

    public class SkeletonFlyweightFactory
    {
        private readonly Dictionary<string, SkeletonModel> _models = new();

        public SkeletonModel GetModel(string type)
        {
            if (!_models.ContainsKey(type))
            {
                _models[type] = new SkeletonModel();
                Console.WriteLine($"Cached new SkeletonModel for {type}.");
            }
            return _models[type];
        }
    }

    public class SkeletonUnit
    {
        private readonly SkeletonModel _model;
        public int X { get; set; }
        public int Y { get; set; }

        public SkeletonUnit(SkeletonModel model, int x, int y)
        {
            _model = model;
            X = x;
            Y = y;
        }

        public void Render() => Console.WriteLine($"Rendering skeleton at ({X}, {Y}) using shared texture {_model.SharedTexture}.");
    }
}

When raising legions of the undead, a naive necromancer will quickly exhaust the physical memory of their phylactery. The Flyweight Swarm optimizes this by caching intrinsic visual data and passing only extrinsic coordinates per unit.

Runic Tags: #structural #flyweight #optimization #csharp