New in Kermeta 2 | |
---|---|
It is possible to declare a class as a singleton. The singleton
works like
companion object
in scala or is similar to a static class in java.
Which means that
only a single instance of the
class exists in memory.
Access to this instance is done directly via the class name.
As any class, the singleton supports operations, attributes, references...
Example 2.6. StdIO singleton
For example, StdIO is now defined as a singleton like this :
singleton StdIO { /** * Writeln the object to standard output */ operation writeln(object : String) : Void is do // code for printing on console end // other operations in StdIO }
It can be directly used like this :
kermeta::io::StdIO.writeln("my message")
Caution | |
---|---|
Be careful when deploying a code that use singleton in environment that uses multiple classpath. For example in OSGI and eclipse, you can (on purpose or not) create several classpathes that aren't shared and create several instances of the same singleton. |