Singleton Class

Help
2011-08-07
2013-04-29
  • Hello,

    Is there a way to specify a singleton class in USE?

    Thanks,
    Mustafa 

     
  • Lars Hamann
    Lars Hamann
    2011-08-16

    Hi Mustafa,

    because USE does not yet support static attributes or operations it is not possible to specify a singelton class using the original pattern.

    What you can do is to specify an invariant which forces the instance count of a class to one:

    context SingletonClass inv singleton:
      SingletonClass.allInstances()->size() = 1
    

    Note, that this constraint is only enforced if an instance of SingletonClass exists. You have to either put the constraint into the context of some "controller class" or you have to use the USE specific extension existential inv:

    context SingletonClass existential inv singleton:
      SingletonClass.allInstances()->size() = 1
    

    This extension can be translated to "there exisits at least one instance which fullfills the condition" in contrast to "for all instances the condition is fullfilled".

    Regards,
    Lars

     
  • Hi Lars,

    Thanks a lot for the detailed answer, I appreciate it.

    Regards,
      Mustafa