I'm making a C# application using the Subject Observer design pattern in a slightly different way.
I am passing the Provider Class, implementing IObservable (this has the OnNext() method that notifies all classes that subscribe to it), to the Observer Class constructor, implementing IObserver (this acts on the OnNext() method posted by the provider class).
That way every single Observer class has the capability of executing the OnNext() method notifying other classes of changes as well.
So a very simplistic look of my sample project goes like:
ProviderClass provider;
ObserverClass observer1;
ObserverClass observer2;
.......... observer3;
.......... observer'n';
main()
{
provider = new ProviderClass();
observer1 = new ObserverClass(provider);
observer2 = new ObserverClass(provider);
observer3 = new ObserverClass(provider);
provider.subscribe(observer1);
provider.subscribe(observer2);
provider.subscribe(observer3);
............
............
// arbitrary code happens here
............
............
// later in code observer1,2,3 can each post OnNext() methods
// that each other observer class will also catch since they all have
//, so with some
// filtering of objects being passed in each OnNext() method
// I think this is a great way of passing data from one class to
// another.
}
class ObserverClass : IObserver<Object>
{
ProviderClass copyInstance;
// Now the observer class can notify other classes using OnNext()
public ObserverClass(Provider copy)
{
copyInstance = copy;
}
public virtual Override OnNext(....)
{
// perform specific action
}
public void notifyClasses(...)
{
copyInstance.OnNext(...);
}
}
Are there any drawbacks to modifying the Subject Observer design pattern this way?