Some criticisms
Did you swallow a dictionary??? ;-)
Seriously though, I did something I considered interesting a while back.
I found that the Properties class is really easy to work with. And its really easy to load a properties file if you know its name. And its really easy to get the name of your class.
So I defined one class that loads properties.
You pass it an object, and it hands you back a properties list from the file which matches that classes name, but with '.properties' as an extension (or whatever you want to use).
So far nothing special, right?
Lets look at an example. We have a bunch of say standard emails we want to send out in response to certain events.
So we've got a properties file like this:
emailpriority=low
emailsubject=Hello World
emailbody=Blah blah blah
So we can create an OutgoingMail abstract class, and in the constructor of that we load in the properties file, and query and assign the values from the properties file to the instance variables of the abstract class.
And in this abstract class we can do most of the work of assigning these values into our SMTP message (see JavaMail api for more details, its really easy)
Now, we subclass the OutgoingMail with different kinds of messages, eg a test message, an emergency message, an we've all been fired, burn down the server room message.... etc.
And then we.... oh wait, we're done.
Note that we didn't do any work in the subclasses constructors... they got all their parameters loaded *for free* by the superclass!!! No extra work in the constructor needed.
--------------------
But... of course there's always a but.
What if you start having so many classes that managing hundreds of properties files becomes a logistical nightmare? Well, you don't have to use your own classes name, you can have a 'designated receiver' class which is there simply to hold a bunch of different classes configuration. And so long as there's no property name conflicts, that will work fine.
What about hierarchical configuration data? This of course puts you back at square one, which is XML. However, you can duplicate some of the hierarchical effect, simply by creating a class hierarchy, though you'd want to be careful that you don't get into a situation where every 'real' class has ten 'fake' classes, just for the sake of configuration.
---------------
Drawbacks:
The configuration solution that I came up with was really hard to sell to the other developer I was working with at the time. He was very conservative and wanted to go with 'the old way of doing things' - which was to lump all the configuration into one file, and use a name mangling scheme, eg:
test-emailbody=...
emerg-emailbody=...
boss-emailbody=...
Which required a couple of pages of code per class, rather than my scheme which required a 'fixed cost' of a couple of pages of code, plus a couple of lines of code per property.
He also had the objection that we couldn't deploy these configuration files in a jar (he didn't understand how to load resources from a jar).... it turned out that the existing applications he'd written deployed their configuration files outside of a jar *anyway*. So that was a non-starter.
The drawback is that its a bit 'radical' and might be a hard sell to very conservative developers.
Posted by: rickcarson on July 14, 2004 at 11:15 PM