Event loops also lend themselves to hashes of anonymous subroutine references (in this case, used as callbacks). Hashes of anonymous subroutines are one of my favorite idioms.

$callbacks = { 567 => sub { }, 568 => sub { }, 569 => sub { } }; # as an exercise for the reader # if defined event id callback, invoke callback # else do what you will to indicate no handler is # registered, log whatever.
callbacks can be keyed off of event ID's, names, or some stringification of a class -- it doesn't matter. Anyhow, this is efficient and very fast, much better than lots of if's, and much cleaner than 100 subclasses of a base event class! It's almost like a bucket sort filled with function pointers. Wheee! (You could do this too, use an array instead of a hash, your choice...)

You may also be interested in POE... but I'm not really a big fan of it.

If the differences between events are subtle and you don't want to repeat code, it may be issue to have a multi-level set of callbacks, whose purpose it is to route events to different types of more specialized event handlers. (Basically seperate your DeathOfWorld events from your ItsAllGood and PleaseHelpMe and IWantACookie events, and then have the handlers for these commands implement a more simplistic callback mechanism.

Thinking in terms of patterns alone will get you roped into spending 80% of your time in the design process. Think in C, Think in C, Think in C, Think in C, there will be an answer, Think in C.


In reply to Re: OT: Design question by SpanishInquisition
in thread OT: Design question by dragonchild

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.