Another approach to singletons - Forum - OpenEdge Development - Progress Community

Another approach to singletons

  • If you want to see some interesting uses for singletons, check out how some languages like Ruby or Smalltalk implement metaclasses. Ruby's metaprogramming abilities that arise from this, in particular, make it quite powerful.

  • jmls wrote:

    to be perfectly honest, I am finding that I am falling out of favour with singletons or statics of any sort, with the possible exception of a single entirely static class consisting of just static properties for things like TerminalID, SessionGUID etc

    IMHO there is no need now to have singletons / statics with the exception of the edge case described in the first sentence.

    I generally agree with limiting the use of singletons and static members - but wanted to point out that these 2 are not interchangable terms. You can create singleton classes that don't use statics.

    -- peter

  • tamhas wrote:

    * Yes, as a general rule, one would prefer that the class enforce its own singletonness.  Obviously.  I offered this simply for those cases where there was a reason why people were reluctant to use statics to create a singleton.   If one does not have those concerns, it is clearly better for the class to do it itself because it is more cohesive, more positive, and avoids the use of a helper class.

    I don't believe types should manage their own lifecycles at all. They do what they're supposed to do, according to the SRP. Someone else's R is to manage their lifecycles.

    -- peter

  • I don't see an object managing its singletonness as the same as it managing its own lifecycle.  Yes, for most objects, someone else decides when it is needed and when it is no longer needed.  But the idea that there is only ever supposed to be one of something is a property of the thing.  It is just that using statics to implement that can have unpleasant sideeffects in some cases.

    Of course, I think there is also a temptation for some people to force something to be a singleton when there is no particular need to do so.  Often, the singletonness is inherent in the object's relationships and there is no need to force.

    Consulting in Model-Based Development, Transformation, and Object-Oriented Best Practice

  • Yes, one can, but it requires a kludge like this technique or the facade/implementation approach I defined on OE Hive to completely avoid statics.

    Consulting in Model-Based Development, Transformation, and Object-Oriented Best Practice