I just started with learning the observer pattern in C# and my requirement is just the opposite of the observer pattern:
We have many processes running and a single process monitoring them all. Each and every process can (if some event happens) notify the parent process.
It seems that there is already this question "What is the opposite of the observer pattern?" which discusses this but still there is no code examples.
For understanding the observer pattern, i referred this code below by Jon Skeet at this answer.
Super-simple example of C# observer/observable with delegates
using System;
class Observable
{
public event EventHandler SomethingHappened;
public void DoSomething()
{
EventHandler handler = SomethingHappened;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
class Observer
{
public void HandleEvent(object sender, EventArgs args)
{
Console.WriteLine("Something happened to " + sender);
}
}
class Test
{
static void Main()
{
Observable observable = new Observable();
Observer observer = new Observer();
observable.SomethingHappened += observer.HandleEvent;
observable.DoSomething();
}
}
Now let us assume we have only one observer (parent process) and many child processes. How does one implement this in observer pattern in c#. Does any other pattern can be used for this implementation ?
Thanks for your answers in advance.