[coldbox:24700] Use of Singletons

Stateless and/or static. That’s my general rule for singletons. If an object needs to change state based on data outside of the APPLICATION or SERVER scopes, extend or use a stateful object that doesn’t persist in those scopes, it doesn’t belong as a singleton. Objects that need injected stateful objects also would be excluded. The only injected objects I would use in a singleton would be Wirebox (which persists in the application scope) or other singletons.

Examples of objects I would use as singletons:

  • File service objects (spreadsheets, images, etc.)
  • Data parsing or manipulation objects
  • Objects which interact with static data or assets (JSON or XML files, images which would not be updated dynamically)
  • Objects which interact with or manage APPLICATION or SERVER scopes

Examples of objects I would never use as singletons:

  • DAO or Gateway objects or service layers that interact with those objects
  • Objects that interact with user sessions, SESSION or REQUEST scopes

HTH,
Jon