What is the best design decision for a ‘top-level’ class to attach to an event to a class that may be ‘5+ layers down in the callstack?
For example, perhaps the MainForm has spawned an object, and that object has spawned a callstack of several other object calls. The most obvious way would be to chain the event up the object hierarchy, but this seems messy and requires a lot of work.
One other solution ive seen is to use the observer pattern by creating a publically accessible static object which exposes the event, and acts as a proxy between the bottom-level object, and the top-level ‘form’.
Any recommendations?
Here’s a pseudo-code example. In this example, the MainForm instantiates ‘SomeObject’, and attaches to an event. ‘SomeObject’ attaches to an object it instantiates, in an effort to carry the event up to the MainForm listener.
class Mainform { public void OnLoad() { SomeObject someObject = new SomeObject(); someObject.OnSomeEvent += MyHandler; someObject.DoStuff(); } public void MyHandler() { } } class SomeObject { public void DoStuff() { SomeOtherObject otherObject = new SomeOtherObject(); otherObject.OnSomeEvent += MyHandler; otherObject.DoStuff(); } public void MyHandler() { if( OnSomeEvent != null ) OnSomeEvent(); } public event Action OnSomeEvent; }
If your application isn’t based on Composite UI Application Blocks, the easiest solution is to put a ‘listener’ class between Main form and your other components which both classes can easily access. Conceptually, the classes are laid out as follows:
Here’s some example code:
This program outputs the following:
This code allows you to invoke methods directly on any listener, no matter how far apart they are in the call stack.
Its easy to rewrite your Listener class so that its a little more generic and works on different types, but you should get the idea.