Singleton: Difference between revisions

Content added Content deleted
No edit summary
Line 737: Line 737:
type Singleton
type Singleton
model
model
text sample
text greeting
fun speak = void by block do writeLine("I'm a singleton") end
fun speak = void by block do writeLine(me.greeting + " I'm a singleton") end
end
end
Singleton instance
Singleton instance
Line 747: Line 747:
type SomeOtherType
type SomeOtherType
Singleton s1 = Singleton.getInstance()
Singleton s1 = Singleton.getInstance()
s1.sample = "Hello"
s1.greeting = "Hello"
Singleton s2 = Singleton.getInstance()
Singleton s2 = Singleton.getInstance()
s2.sample.append(", World!")
s2.greeting.append(", World!")
writeLine(s1 + " and " + s2 + " are the same object: " + (s1 == s2) + ", " + s2.sample)
writeLine(s1 + " and " + s2 + " are the same object: " + (s1 == s2) + ", s2: " + s2.greeting)
s1.speak() # call instance method
s1.speak() # call instance method
</syntaxhighlight>
</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
§(0x02bf8098) and §(0x02bf8098) are the same object: ⊤, Hello, World!
§(0x02bf8098) and §(0x02bf8098) are the same object: ⊤, s2: Hello, World!
I'm a singleton
Hello, World! I'm a singleton
</pre>
</pre>