Publisher and Subscribers - What they do?
To understand event, we generally consider two classes- publisher and subscriber.
- The publisher class defines the condition under which an event is raised.
- Subscriber classes only decide what to do when the event happens. They do not control when the event will occur.
- Publisher class controls when the event will be raised. This condition is defined inside a method usually named like OnEventOccured, OnEventRaised, OnAlarmRaised, OnBellRings etc. This is common naming convention for Event raising method but you can use any other style name as well.
- The subscribers classes must subscribe to the event so that when event is raised they are notified and event handling methods could be executed. Note that more than one event handler method can be attached with the event.
- Event subscribing is done using += operator. The operator takes event as left operand and event handler method as its right operand.
- Event can be de-subscribed using -= operator, similar to += operator.
- Only after subscribing/desubscribing, event can be raised by calling e.g. OnEventRaised method.
- Raising an event without subscription means event will be null. Therefore, generally, OnEventRaise method checks if event is null or not, before invoking the event. I have ignored this in the example for simplicity. If wish, your code should be: if(TemperatureExceeded !=null){TemperatureExceeded?.Invoke();}
- So, event subscribing is nothing but event initialization with event handler method.
Look at the following example.
- TemperatureSensor is Event Publisher class.
- Alarm is Event Subscriber class.
- The Alarm class has Ring method which is attached with event using += operator. It means that when event will be raised then Ring method will be called.
- Publisher class has a method which will invoke the event with condition, temperature value more than 100. In our example, the temperature value is checked before raising the event.
Example:
Part1
// TemperatureSensor is publisher class with event member
// and a method, CheckTemperature that raises the event
class TemperatureSensor
{
public event Action? TemperatureExceeded;
public void CheckTemperature(int temp)
{
// Publisher decides the condition
if (temp > 100)
{
TemperatureExceeded?.Invoke();
}
}
}
Part2
// Alarm is a Subscriber class
// that contains event handler method, Ring
class Alarm
{
public void Ring()
{
Console.WriteLine("Temperature too high!");
}
}
Part3
// Program is client class that uses publisher and subscriber class
// to attach event of publisher class with handler of subscriber class
// using += operator
// and then calls the On event raised method i.e. CheckTemperature
class Program
{
static void Main()
{
TemperatureSensor sensor = new TemperatureSensor();
Alarm alarm = new Alarm();
// Subscriber registers
sensor.TemperatureExceeded += alarm.Ring;
sensor.CheckTemperature(90); // Nothing
sensor.CheckTemperature(110); // Event raised
}
}
No comments:
Post a Comment