Multiton: Difference between revisions

1,210 bytes added ,  2 months ago
Added Python
m (syntax highlighting fixup automation)
(Added Python)
 
(One intermediate revision by one other user not shown)
Line 176:
"two_544"
</pre>
 
=={{header|Python}}==
{{works with|Python|3.x}}
<syntaxhighlight lang="python">#!/usr/bin/python
 
import threading
 
class Multiton:
def __init__(self, registry, refnum, data=None):
with registry.lock:
if 0 < refnum <= registry.max_instances and registry.instances[refnum] is None:
self.data = data
registry.instances[refnum] = self
elif data is None and 0 < refnum <= registry.max_instances and isinstance(registry.instances[refnum], Multiton):
self.data = registry.instances[refnum].data
else:
raise Exception("Cannot create or find instance with instance reference number {}".format(refnum))
 
class Registry:
def __init__(self, maxnum):
self.lock = threading.Lock()
self.max_instances = maxnum
self.instances = [None] * maxnum
 
reg = Registry(3)
m0 = Multiton(reg, 1, "zero")
m1 = Multiton(reg, 2, 1.0)
m2 = Multiton(reg, 1)
m3 = Multiton(reg, 2)
 
for m in [m0, m1, m2, m3]:
print("Multiton is {}".format(m.data))
 
 
# produce error
#m2 = Multiton(reg, 3, [2])
 
# produce error
# m3 = Multiton(reg, 4, "three")
 
# produce error
# m5 = Multiton(reg, 5)</syntaxhighlight>
 
=={{header|Raku}}==
Line 220 ⟶ 262:
 
Although all Wren code runs within the context of a fiber (of which there can be thousands) only one fiber can run at a time and so the language is effectively single threaded. Thread-safety is therefore never an issue.
<syntaxhighlight lang="ecmascriptwren">import "./dynamic" for Enum
 
var MultitonType = Enum.create("MultitonType", ["zero", "one", "two"])
2,122

edits