вторник, 17 февраля 2026 г.

Simple, EventHub

Simple, EventHub

// Interfaces.cs

public interface IPublisherService

{

    void Publish(string ticker, string message);

}


public interface ISubscriberService

{

    void Subscribe(string ticker, Action<string> handler);

}


public class EventHub : Dictionary<string, List<Action<string>>>

{

    public void AddHandler(string ticker, Action<string> handler)

    {

        if (!ContainsKey(ticker))

            this[ticker] = new List<Action<string>>();

        

        this[ticker].Add(handler);

    }

    

    public void InvokeHandlers(string ticker, string message)

    {

        if (TryGetValue(ticker, out var handlers))

        {

            foreach (var handler in handlers)

                handler(message);

        }

    }

}

Комментариев нет:

Отправить комментарий