Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I have been tasked with rebuilding a system to track widgets (what I'm tracking is irrelevant). This subsystem is an XML-RPC server with a very well-defined API. Whenever a new widget is added (through the add_widget() call), there are potentially N actions that need to be taken after the widget has been successfully inserted into the database. For example, if a green widget is added, a specific person needs to be emailed, or whatever.

Every widget is based on a widget type. Each widget type has a specific number of properties (color, size, etc). The events trigger based on the properties of the widget type.

Now, the events will be pre-made, but there are currently over a hundred events. Some widget types have no events that trigger on them, but some might potentially trigger up to 5 (or more) events. There is no constraint on the number of events the addition of a given widget might trigger.

The current system is built around a huge if-elsif-else statement that is around 500 lines long, most of which is cut'n'pasted. There's only really about 4-5 different types of events, just with different parameters. The business wants to triple the number of widget types being handled, adding at least another 10-20 different types of events. (It's important to note that each event type will take completely different parameters.)

So, it's relatively obvious that the if-elsif-else option isn't viable in the long-term. I was thinking about creating an event system, based on the following high-level design:

  1. The widget gets inserted into the database
  2. There will be a bunch of Event objects/classes, each providing a wants_this() and a handle_this() method.
  3. The XML-RPC server will call each Event's wants_this() method, passing in all the relevant information. If the methodcall returns true, then the handle_this() method will be called, passing in all the relevant information.
  4. The Event will go ahead and do the necessary actions, such as emailing the specific person.

Here's the problem - how do I structure this? Assume that the people who will maintain this are Perl gurus, so any feature is usable. My thoughts are going down the following paths:

  • There will be an Event class for each possible event. There will be a base class for each Event type. All the Events will be in a particular directory and all the events will be pre-loaded at server startup.
  • There will be an Event object for each possible event. There will be a base class for each Event type. All the Event objects will be configured at server startup. (Do I use a config file? A set of DB tables?)
  • Something else?

It's a messy situation. If I go with objects and a config file, I have to specify the event type and all the necessary parameters. Do I just die if the config file is mis-configured? If I go with the database option, how do I source-control it?

If I go with a class for each specific event, I now have hundreds of classes, most of which will look extremely similar. That sounds even worse than the if-elsif-else option!

Help!

Update: I don't think people are understanding the question I'm trying to ask. I already know I'm going to be using a set of callbacks for the event handlers. Those callbacks are going to register with the add_widget() function in some fashion. My question is how to deal with the hundreds of callbacks I'm going to have to deal with. Read the exchange I had with Joost for what I mean.

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.


In reply to 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-19 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found