1

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?

  • As a side note try to follow C# code conventions. – TIKSN Mar 28 '15 at 20:11
1

It looks like you are using your observer as both a publisher and a subscriber, while the Provider is only a publisher. Or is it both too? This seems a bit confused.

Maybe you should make some kind of Bus or Queue that you can both Publish and Subscribe to. There are so many complexities in messaging that you really should make a distinct component be responsible for handling only that.

  • My goal was to have a c# project have n classes be instantiated in the main class. Then any data that needs to be passed from one class to another can use the OnNext method, and the other class with some filtering can process that data. – CD VA Programmer Apr 8 '15 at 0:46
0

Are there any drawbacks to modifying the Subject Observer design pattern this way?

Well, yeah.

  • What I see is backwards, inside-out; an incorrect implementation

    • The observer (ObserverClass) should not be the notifier - the notifier is your ProviderClass. The notifier notifies, the observer observes.
    • Having the ObserverClass calling the providers methods makes no sense.
    • The code is setting up a circular reference, apparently. The provider calls the observer that in turn calls the provider's method, which in turn ...
  • If you want to "chain" observable the a class can implement both IObservable and IObserver.

Are there any drawbacks to modifying the Subject Observer design pattern this way?

Well, yeah.

  • What I see is backwards, inside-out; an incorrect implementation

    • The observer (ObserverClass) should not be the notifier - the notifier is your ProviderClass. The notifier notifies, the observer observes.
    • Having the ObserverClass calling the providers methods makes no sense.
    • The code is setting up a circular reference, apparently. The provider calls the observer that in turn calls the provider's method, which in turn ...
  • If you want to "chain" observable the a class can implement both IObservable and IObserver.

RE: 4/8/2015 comment

one class reads in files and searches for a keyword

I sense a single responsibility principle problem. Generally I see I/O functionality as a "single responsibility". Where to put the searching is dependent on the overall design.

the second class handles emailing the owner of the program ... [of the found keyword].

That sounds fine. I'm going to assume below that finding a keyword is the event trigger.

If both classes were instantiated in the main c# class then ...

You have a good place to coordinate the activities. The OO goodness here is that the Observer and the Observable do not have to really know about each other. Class types and event delegate signatures will ensure they can work together.

So "the main C# class" will:

  1. Register an IObserver object's event handler on its behalf to the IObservable.
  2. I suspect it may take these objects as constructor/method parameters.

  3. Call to the I/O class to fetch the file.

  4. Pass the file to the method/class that searches - this is the IObservable, if I'm guessing right about the intent.

  5. Whatever does the search triggers the event when the keyword is found.

  6. May un-register the event handler as well. Depends on object life cycle, design, intent, etc.

End RE: 4/8/2015 comment

  • The reason I did implement it as such is because say you have to classes with distinct behaviors. For example, one class reads in files and searches for a keyword, the second class handles emailing the owner of the program the searched keyword. If both classes were instantiated in the main c# class then the only way I can think of passing data from the searching file class to the email class is by having the subject-observer pattern slightly modified. – CD VA Programmer Apr 8 '15 at 0:42
0

I pretty much agree with what @radarbob answer states. For some time I wondered as well, about how one would implement such a design pattern in "real life". It's fine when reading about it to understand the design pattern theoretically, but actually applying it is different.

If you have access to Pluralsight I would highly recommend the following video (worth signing up with a trial for):

Tactical design patterns in .NET

I think the issues that he addresses and topics he talk about (specifically about the observer pattern as well), would benefit you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.