The Source for Java Technology Collaboration
User: Password:



Start New Message Delete Post a Reply

Article: 
 Breaking the Last Dependency
Subject:  Sorry, a map based registry would have been better
Date:  2005-09-07 08:13:15
From:  christian_schlichtherle


Hi,

nice idea, but why did you introduce a weak spot with a properties file?

A much better and even simpler way would be to:

1. Add a (hash)map to your factory mapping from strings (actor descriptions) to class objects.

2. Add a static method to your factory class which allows other classes to register themselves with their actor name and their class object. The method would simply add this information to the map.

3. Add a static initializer to your actor classes calling the registry method in step 2.

4. Instead of looking up a properties file, lookup the map in the factory method to get the class object.

This entirely removes the weak spot because actor classes register themselves at runtime.

The method in step 2 should also check that any registered class really implements the actor interface. This break-early-approch adds much more reliablity.

Regards,
Christian

 Feed java.net RSS Feeds