Defining event handlers for function, method, or shared method

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 26 10:41:34 PST 2016


In many multi threading module designs of mine, I generally 
design a base class, and
this class have some events. Exempli gratia:

void eventOnStart();
void eventOnStop();
void eventOnItemAdded( size_t itemIndex );

There is this problem though. I need/want this class to be able 
to bind a function, a method, or a shared method. From the 
perspective of class design, there shouldn't be any
difference. Its purpose is to let know about the event, not to 
care about how the event
handler is designed.

If I want handlers to be functions, I design it like,

public alias EventOnStart = void function();
public EventOnStart eventOnStart;


If it is for normal methods, design becomes like,

public alias EventOnStart = void delegate();


For shared methods, it becomes,

public alias EventOnStart = void delegate() shared;


As you will guess, to be able to support any of those three, it 
becomes so complex. Is
there any way generalise to support three of them without making 
this any complex? A
secondary thing, this is not D related though, whether there is 
any different approach
for event listener design like Observer pattern but with little 
overhead and complexity?


More information about the Digitalmars-d-learn mailing list