Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
27
Task/Singleton/Forth/singleton.fth
Normal file
27
Task/Singleton/Forth/singleton.fth
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
include FMS-SI.f
|
||||
|
||||
\ A singleton is created by using normal Forth data
|
||||
\ allocation words such as value or variable as instance variables.
|
||||
\ Any number of instances of a singleton class may be
|
||||
\ instantiated but they will all operate on the same shared data.
|
||||
\ The data name space will remain private to objects of the class.
|
||||
|
||||
:class singleton
|
||||
0 value a
|
||||
0 value b
|
||||
:m printa a . ;m
|
||||
:m printb b . ;m
|
||||
:m add-a ( n -- ) a + to a ;m
|
||||
:m add-b ( n -- ) b + to b ;m
|
||||
;class
|
||||
|
||||
singleton s1
|
||||
singleton s2
|
||||
singleton s3
|
||||
|
||||
4 s1 add-a
|
||||
9 s2 add-b
|
||||
s3 printa \ => 4
|
||||
s3 printb \ => 9
|
||||
s1 printb \ => 9
|
||||
s2 printa \ => 4
|
||||
Loading…
Add table
Add a link
Reference in a new issue