If you are looking in a design perspective, where you must write a
program in C# that needs to do something else when an event appear (callback). How would you then prefer to make the archittecture ? With Delegates or virtual functions ?
This allows your worker objects to subscribe to a publisher object. Once something happens, the publisher will raise the relevant event, and all the workers, that subscribed to the event, will be notified.
A common and recommended design pattern in .NET is to have events raised by On[eventname] functions in the class. These routines should be virtual, so one can hook events externally or actually derive from the class and override. One can also then not call the base method and avoid the overhead of the delegates being called.
I have now been reading this book. All along I have been looking into many things and have decided that a baseclass and a container class with some event was the best solution. Until I read this book. Here they recommand to use Observer pattern with delegates and event instead of a base class and an observer interface if you only have single inheritance. Furthermore they write there is no need for Subject, Subjecthelper and observer because the common language runtime makes them obsolete. This means that the subject that is a baseclass goes out !!!!! I do not get it!
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.