Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
10
Task/Singleton/Ada/singleton-1.adb
Normal file
10
Task/Singleton/Ada/singleton-1.adb
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
package Global_Singleton is
|
||||
procedure Set_Data (Value : Integer);
|
||||
function Get_Data return Integer;
|
||||
private
|
||||
type Instance_Type is record
|
||||
-- Define instance data elements
|
||||
Data : Integer := 0;
|
||||
end record;
|
||||
Instance : Instance_Type;
|
||||
end Global_Singleton;
|
||||
21
Task/Singleton/Ada/singleton-2.adb
Normal file
21
Task/Singleton/Ada/singleton-2.adb
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package body Global_Singleton is
|
||||
|
||||
--------------
|
||||
-- Set_Data --
|
||||
--------------
|
||||
|
||||
procedure Set_Data (Value : Integer) is
|
||||
begin
|
||||
Instance.Data := Value;
|
||||
end Set_Data;
|
||||
|
||||
--------------
|
||||
-- Get_Data --
|
||||
--------------
|
||||
|
||||
function Get_Data return Integer is
|
||||
begin
|
||||
return Instance.Data;
|
||||
end Get_Data;
|
||||
|
||||
end Global_Singleton;
|
||||
11
Task/Singleton/Ada/singleton-3.adb
Normal file
11
Task/Singleton/Ada/singleton-3.adb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package Protected_Singleton is
|
||||
procedure Set_Data (Value : Integer);
|
||||
function Get_Data return Integer;
|
||||
private
|
||||
protected Instance is
|
||||
procedure Set(Value : Integer);
|
||||
function Get return Integer;
|
||||
private
|
||||
Data : Integer := 0;
|
||||
end Instance_Type;
|
||||
end Protected_Singleton;
|
||||
47
Task/Singleton/Ada/singleton-4.adb
Normal file
47
Task/Singleton/Ada/singleton-4.adb
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package body Protected_Singleton is
|
||||
|
||||
--------------
|
||||
-- Set_Data --
|
||||
--------------
|
||||
|
||||
procedure Set_Data (Value : Integer) is
|
||||
begin
|
||||
Instance.Set(Value);
|
||||
end Set_Data;
|
||||
|
||||
--------------
|
||||
-- Get_Data --
|
||||
--------------
|
||||
|
||||
function Get_Data return Integer is
|
||||
begin
|
||||
return Instance.Get;
|
||||
end Get_Data;
|
||||
|
||||
--------------
|
||||
-- Instance --
|
||||
--------------
|
||||
|
||||
protected body Instance is
|
||||
|
||||
---------
|
||||
-- Set --
|
||||
---------
|
||||
|
||||
procedure Set (Value : Integer) is
|
||||
begin
|
||||
Data := Value;
|
||||
end Set;
|
||||
|
||||
---------
|
||||
-- Get --
|
||||
---------
|
||||
|
||||
function Get return Integer is
|
||||
begin
|
||||
return Data;
|
||||
end Get;
|
||||
|
||||
end Instance;
|
||||
|
||||
end Protected_Singleton;
|
||||
|
|
@ -1,17 +1,17 @@
|
|||
type Singleton
|
||||
model
|
||||
text greeting
|
||||
fun speak = void by block do writeLine(me.greeting + " I'm a singleton") end
|
||||
fun speak ← void by block do writeLine(me.greeting + " I'm a singleton") end
|
||||
end
|
||||
Singleton instance
|
||||
fun getInstance = Singleton by block
|
||||
if instance == null do instance = Singleton() end
|
||||
fun getInstance ← Singleton by block
|
||||
if instance æ null do instance ← Singleton() end
|
||||
return instance
|
||||
end
|
||||
type SomeOtherType
|
||||
Singleton s1 = Singleton.getInstance()
|
||||
s1.greeting = "Hello"
|
||||
Singleton s2 = Singleton.getInstance()
|
||||
Singleton s1 ← Singleton.getInstance()
|
||||
s1.greeting ← "Hello"
|
||||
Singleton s2 ← Singleton.getInstance()
|
||||
s2.greeting.append(", World!")
|
||||
writeLine(s1 + " and " + s2 + " are the same object: " + (s1 == s2) + ", s2: " + s2.greeting)
|
||||
writeLine(s1 + " and " + s2 + " are the same object: " + (s1 æ s2) + ", s2: " + s2.greeting)
|
||||
s1.speak() # call instance method
|
||||
|
|
|
|||
19
Task/Singleton/Pluto/singleton-1.pluto
Normal file
19
Task/Singleton/Pluto/singleton-1.pluto
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
-- Singleton.pluto --
|
||||
|
||||
local instance = nil
|
||||
|
||||
class singleton
|
||||
static function getInstance()
|
||||
if !instance then new singleton() end
|
||||
return instance
|
||||
end
|
||||
|
||||
function __construct()
|
||||
assert(!instance, "The singleton instance already exists.")
|
||||
instance = self
|
||||
end
|
||||
|
||||
function print()
|
||||
print("Hello, I'm a singleton.")
|
||||
end
|
||||
end
|
||||
12
Task/Singleton/Pluto/singleton-2.pluto
Normal file
12
Task/Singleton/Pluto/singleton-2.pluto
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
-- Singleton_user.pluto --
|
||||
|
||||
require "Singleton"
|
||||
|
||||
local s = singleton.getInstance()
|
||||
s:print()
|
||||
local t = singleton.getInstance()
|
||||
t:print()
|
||||
print(s == t) -- true, as same object
|
||||
print(instance) -- nil
|
||||
local u = new singleton() -- throws an error
|
||||
u:print() -- not executed
|
||||
Loading…
Add table
Add a link
Reference in a new issue