Announcement Announcement Module
Collapse
No announcement yet.
Persisting singletons with state Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Persisting singletons with state

    I'm looking for an architecture best practice for persisting singletons with state. I'm sure this comes up often enough, and maybe I'm just too tired to see the obvious solution. An example:

    I have a class CCB (change control board, for those not working within bureaucracies):

    Code:
    public class CCB {
        private User chair;
        private Set<User> voters;
    	
        // getters & setters
    }
    This should be a singleton, as there is only one CCB. An application admin should be able to change the content of voters through a web interface, and have those changes persisted to a database (I'm using Hibernate).

    The solutions that come to mind seem inelegant or restrictive to me. If I didn't need to allow users to modify state, I could create a Spring bean, with a property editor for User, and put it all in an appContext.xml. But that's not an option. I could also give CCB an id property, and hard code that id into the CCB DAO (or put the id into the appContext.xml and inject it into a CCB service bean). Again, seems a little heavy handed.

    How is this normally implemented?

    Thanks in advance,
    Philip

  • #2
    I'm not quite sure what the problem is here. If you want only one of them, then create and initial one in the database and don't allow a new one to be created in the code.

    Comment

    Working...
    X