As promised here is the version of observer pattern with Event & Delegate. Personally I am not a big fan of this solution, although it does remove quite a bit of code and show how to use a push model with the observer pattern. Next time around I will show a more elegant solution by using Event Aggregator.

But for now lets see how I implemented Observer Pattern with Event/Delegate.

First I tackled the Subject of our class, the thing you will see different is there is an instance object called TextChanged and is an EventHandler, the second change is it Notify method. I also removed the ISubject interface just because there isn’t really a need for it in this example and to make things simple.

 

From the above code you will see that there is an object called TextLangChangedEventArgs, that is basically our data object that would be pushed to the observer. Here is how it looks like, the important part is that it extends/inherits from EventArgs

 

Finally back to our WPFApp, we again have 2 labels the Turkish and English one, but they both don’t know anything about the Subject which is a good thing, so that it is loosely coupled. In order for the subject to hook into them they provide a signature where the event will call it.

 

Turkish UserControl does the translation of English to Turkish

 

Last but not least here is the WPFApp

 

I have not posted the XAML code but one can download the source and solution.

For next time I would like to show a more elegant solution called Event Aggregator which is a pretty cool pattern introduced by Martin Flower that allows one to write extensible code.