dragonchild has asked for the wisdom of the Perl Monks concerning the following question:
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:
- The widget gets inserted into the database
- There will be a bunch of Event objects/classes, each providing a wants_this() and a handle_this() method.
- 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.
- 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.