Singleton: Difference between revisions

Content added Content deleted
Line 1,073: Line 1,073:
return sharedInstance;
return sharedInstance;
}</lang>
}</lang>

===With class methods===
It's possible to accomplish the same thing with class methods of some class, rather than instance methods on the instance of a singleton class. Data that needs to be kept as "instance variables" would instead be kept as <code>static</code> (file-local) global variables. "Initialization" of the singleton object would be done in the <code>+initialize</code> method, which is guaranteed to be called at most once for every class, the first time the class is messaged. This way, the singleton is also "lazy loaded" as needed.

In other words, here the class object serves as the singleton object. The "singleton class" is the metaclass of the class. The downside of this approach is that the "singleton class" (the metaclass of the class) cannot be made to explicitly inherit from a class of the user's choice, or implement a protocol of the user's choice. Also, there is no way to prevent subclasses of the class from being made, thus effectively creating "multiple instances" of the singleton class. Also, one cannot declare properties on the singleton (the class object).


=={{header|ooRexx}}==
=={{header|ooRexx}}==