Добрый день, кто-то может подсказать как реализовать PubSub Event channel паттерн на ES6?
Реализацию данного паттерна видел только на Java, но не понимаю как без интерфейсов переписать это на js, так как их в языке нет.
Пример на java
public interface IEventChannel
{
void Publish(string topic, string data);
void Subscribe(string topic, ISubscriber subscriber);
}
public interface IPublisher
{
void Publish(string data);
}
public interface ISubscriber
{
void Notify(string data);
}
public class EventChannel: IEventChannel
{
private Dictionary<string, List<ISubscriber>> _topics =
new Dictionary<string, List<ISubscriber>>();
public void Publish(string topic, string data)
{
if(!_topics.ContainsKey(topic)) return;
foreach(var subscriber in _topics[topic])
subscriber.Notify(data);
}
public void Subscribe(string topic, ISubscriber subscriber)
{
if(_topics.ContainsKey(topic))
_topics[topic].Add(subscriber);
else
_topics.Add(topic, new List<ISubscriber>() { subscriber });
}
}
public class Publisher: IPublisher
{
private string _topic;
private IEventChannel _channel;
public Publisher(string topic, IEventChannel channel)
{
_topic = topic;
_channel = channel;
}
public void Publish(string data)
{
_channel.Publish(_topic, data);
}
}
public class Subscriber: ISubscriber
{
private string _name;
public Subscriber(string name)
{
_name = name;
}
public void Notify(string data)
{
Console.Write($"Subscriber '{_name}' notify: '{data}'")
}
}
static class Program
{
public void Main(string[] args)
{
var channel = new EventChannel();
var publisherA = new Publisher("#topic.a", channel);
var publisherB = new Publisher("#topic.b", channel);
var subscriberA = new Subscriber("Reader 1");
var subscriberB = new Subscriber("Reader 2");
channel.Subscribe("#topic.a", subscriberA);
channel.Subscribe("#topic.a", subscriberB);
channel.Subscribe("#topic.b", subscriberB);
// Console write: Subscriber 'Reader 1' notify: 'Text1'
// Console write: Subscriber 'Reader 2' notify: 'Text1'
publisherA.Publish("Text1");
// Console write: Subscriber 'Reader 2' notify: 'Text2'
publisherB.Publish("Text2");
}
}