♯ C# EVOCATION DIFFICULTY: Apprentice Behavioral

The Observer Scrying

Defining a one-to-many dependency between objects.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Observer
ARCANUM SCHOOL: Divination // Scrying Networks
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CSHARP PATTERN //
using System;
using System.Collections.Generic;

namespace EnterpriseEvocation
{
    public interface IScryer
    {
        void Update(string cosmicEvent);
    }

    public class Observatory
    {
        private readonly List<IScryer> _scryers = new();

        public void Attach(IScryer scryer) => _scryers.Add(scryer);

        public void CosmicShift(string @event)
        {
            Console.WriteLine($"Observatory detects: {@event}");
            foreach (var scryer in _scryers)
            {
                scryer.Update(@event);
            }
        }
    }

    public class Astromancer : IScryer
    {
        public void Update(string cosmicEvent) => Console.WriteLine($"Astromancer notes the {cosmicEvent}.");
    }
}

Instead of having every astromancer constantly poll the heavens, the Observatory simply notifies all registered IScryers when a cosmic shift occurs. In modern C#, this is often supplanted by native event delegates, but the pure interface Observer retains archaic elegance.

Runic Tags: #behavioral #observer #events #csharp