Design Pattern Quandry
March 20, 2008 2:30 PM
Subscribe
Programming/design pattern question. Being self-taught, I've discovered a number of times I've labored over the solution to a problem only to discover a more elegant, standard design pattern already addresses the problem. I'm sort of hoping this is the case here.
What I want to do is provide a way for users to arbitrarily combine given numbers to generate meaningful-to-them numbers and have the means of doing this combining persist. For example, the application I have in mind stores counting statistics in baseball by player and date. As a user of this application, I'd like to be able to combine these stats in various ways that are more or less arbitrary from the application's POV. Many common formulas can be pre-programmed, i.e. OBP = (Hits + Walks + HBP ) / (AB + Sac + Walks + HBP). However, many may not be so easily anticipated, i.e. Quality Start = if( GS && (IP >= 6) && (ER <= 3) ) then return 1.
Instead of having to create a new class (oh, I'm using Java here) every time a user develops a new stat they'd like generated/displayed/scored in fantasy baseball, I'd like to develop a way to generate these classes/objects via the ui and then re-use them in subsequent user sessions. I can see how to do this in the UI using drag/drop or drop-down menus, but I don't know how to model this on the back end.
My present model suggests a class StatLine that is essentially a Collection of an arbitrary number of objects that implement a Stat interface. This interface describes methods like set/get value, get title/label, and get/set formula in addition to some other bookkeeping kinds of things like how to sort for scoring (ascending vs. descending). How to represent, store, and retrieve arbitrary formulas is the part that has me spinning in circles. It would seem that this is an ideal situation to use some combination of a properties file and reflection but the means have not yet come to me.
Something along the lines of:
public class AVG implements Stat {
public AVG( Stat Hits, Stat AB ) {
...
}
.... all kinds of getters and setters....
public Double getValue() {
...read some property that represents the formula and feed that formula to some kind of engine that returns the computed value...
}
}
It could be that I should drop the idea of making unique classes that implement the Stats interface for each statistic generated. Instead one could monkey together an engine that turned a string in to a calculation that is stored as a field in a Stats object. It just doesn't feel right though.
If you have run in to a pattern like this how did you solve it? What, if any, name does this design pattern have? What, if any, resources would you recommend for asking this question to a more specific audience?
posted by Fezboy! to computers & internet (12 comments total)
5 users marked this as a favorite
posted by pharm at 3:03 PM on March 20, 2008