Class Mediator
- Namespace
- WitShells.DesignPatterns.Core
A concrete, event-bus style Mediator implementation. Components subscribe to named event channels and are notified whenever another component calls Notify(object, string, object).
public class Mediator : IMediator
- Inheritance
-
Mediator
- Implements
- Inherited Members
Examples
var mediator = new Mediator();
mediator.Subscribe("OnScoreChanged", (sender, data) => Debug.Log($"Score: {data}"));
mediator.Notify(this, "OnScoreChanged", 42);
Remarks
Useful for cross-system communication (e.g. UI reacting to game-world events) without creating direct dependencies between the systems.
Methods
Notify(object, string, object)
Broadcasts an event to all subscribers registered under eventKey.
public void Notify(object sender, string eventKey, object data = null)
Parameters
senderobjectThe object raising the event.
eventKeystringA string identifier for the event channel (e.g.
"OnPlayerDied").dataobjectOptional payload accompanying the event.
Subscribe(string, Action<object, object>)
Subscribes callback to the named event channel.
public void Subscribe(string eventKey, Action<object, object> callback)
Parameters
eventKeystringThe event channel identifier.
callbackAction<object, object>Callback invoked with (sender, data) when the event fires.
Unsubscribe(string, Action<object, object>)
Removes callback from the named event channel.
public void Unsubscribe(string eventKey, Action<object, object> callback)