• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singletons & application design question  RSS feed

 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a singleton accept arguments in its constructor without being broken?

e.g. - I have a singleton implementing a data access interface. This implementation grabs all the data from an XML file and parses it, resulting in a couple of Maps. There are two public methods: getUserMap() and getObjectMap(). Each will check if the singleton's Maps have been populated, if not it will load and parse the XML file. If so it will will just return them. However, I need some way for my singleton to find the name of the XML file to parse. Objects that call getObjectMap() or getUserMap() won't know of care the name of the XML file - only the main application class will know this.

Suggestions?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, a singleton can accept parameters to the constructor or even have setXxx mehtods to load configuration or change options in a running system. Or your singleton could read configuration in its constructor.

Even better, I like to have an "application assembler" that runs once at startup and initializes configuration and object relationships. Unit tests can do custom test configuration assembly which can be very cool.
 
Tom McAmmond
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you - that's a good suggestion. So, my application will simply initialize the singleton upon startup with the location of my datastore (in this case, the filename of my XML configuration file).

I suppose the getMap() methods could simply throw an UnsupportedOperationException if they find that the singleton wasn't initialized for some reason. Can you think of a better way of handling such a condition? (someone else may one day tack on a different frontend, and try to use the singleton without initializing it).

I appreciate the benefit of your experience
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some kind of spectacular failure would be good if they forgot to initialize it. I like things like that to fail fast and loud. Heh heh.

A short assembler example ... there is a lot of code like this:

The assembler for my home-made Wiki only runs once at startup. But my work project is built so I can re-run the configurator while the system is running. We use it to set logging and debugging options, enable and disable services, change the URLs for partner web systems, all kinds of things. Update an XML file or a config table in the database, reload the configuration, clear some caches and we're a whole new system.

If you really like this kind of "dependency injection" the Spring framework was built on it as a core concept. It's very cool to see a bunch of classes with no real dependencies on each other, only interfaces.
[ June 24, 2005: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!