2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,2 +1,4 @@
A Global Singleton is a class of which only one instance exists within a program.
Any attempt to use non-static members of the class involves performing operations on this one instance.
<br><br>

View file

@ -0,0 +1,4 @@
#symbol singleton =
{
// ...
}.

View file

@ -0,0 +1,8 @@
#class $Singleton
{
#field theField.
// ...
}
#static singleton = $Singleton new.

View file

@ -3,8 +3,9 @@ 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.
\ instantiated but messages will all operate on the same shared data
\ so it is the same as if only one object has been created.
\ The data name space will remain private to the class.
:class singleton
0 value a

View file

@ -1,9 +1,31 @@
package singlep
package main
// package level data declarations serve as singleton instance variables
var X, Y int
import (
"log"
"math/rand"
"sync"
"time"
)
// package level functions serve as methods for a package-as-a-singleton
func F() int {
return Y - X
var (
instance string
once sync.Once // initialize instance with once.Do
)
func claim(color string, w *sync.WaitGroup) {
time.Sleep(time.Duration(rand.Intn(1e8))) // hesitate up to .1 sec
log.Println("trying to claim", color)
once.Do(func() { instance = color })
log.Printf("tried %s. instance: %s", color, instance)
w.Done()
}
func main() {
rand.Seed(time.Now().Unix())
var w sync.WaitGroup
w.Add(2)
go claim("red", &w) // these two attempts run concurrently
go claim("blue", &w)
w.Wait()
log.Println("after trying both, instance =", instance)
}

View file

@ -1,14 +1,14 @@
package main
package singlep
import (
"fmt"
"singlep"
)
// package level data declarations serve as singleton instance variables
var X, Y int
func main() {
// dot selector syntax references package variables and functions
singlep.X = 2
singlep.Y = 3
fmt.Println(singlep.X, singlep.Y)
fmt.Println(singlep.F())
// package level initialization can serve as constructor code
func init() {
X, Y = 2, 3
}
// package level functions serve as methods for a package-as-a-singleton
func F() int {
return Y - X
}

View file

@ -0,0 +1,12 @@
package main
import (
"fmt"
"singlep"
)
func main() {
// dot selector syntax references package variables and functions
fmt.Println(singlep.X, singlep.Y)
fmt.Println(singlep.F())
}

View file

@ -0,0 +1,24 @@
package single
import (
"log"
"sync"
)
var (
color string
once sync.Once
)
func Color() string {
if color == "" {
panic("color not initialized")
}
return color
}
func SetColor(c string) {
log.Println("color initialization")
once.Do(func() { color = c })
log.Println("color initialized to", color)
}

View file

@ -0,0 +1,12 @@
package red
import (
"log"
"single"
)
func SetColor() {
log.Println("trying to set red")
single.SetColor("red")
}

View file

@ -0,0 +1,12 @@
package blue
import (
"log"
"single"
)
func SetColor() {
log.Println("trying to set blue")
single.SetColor("blue")
}

View file

@ -0,0 +1,24 @@
package main
import (
"log"
"math/rand"
"time"
"blue"
"red"
"single"
)
func main() {
rand.Seed(time.Now().Unix())
switch rand.Intn(3) {
case 1:
red.SetColor()
blue.SetColor()
case 2:
blue.SetColor()
red.SetColor()
}
log.Println(single.Color())
}

View file

@ -0,0 +1,29 @@
import abc
class Singleton(object):
"""
Singleton class implementation
"""
__metaclass__ = abc.ABCMeta
state = 1 #class attribute to be used as the singleton's attribute
@abc.abstractmethod
def __init__(self):
pass #this prevents instantiation!
@classmethod
def printSelf(cls):
print cls.state #prints out the value of the singleton's state
#demonstration
if __name__ == "__main__":
try:
a = Singleton() #instantiation will fail!
except TypeError as err:
print err
Singleton.printSelf()
print Singleton.state
Singleton.state = 2
Singleton.printSelf()
print Singleton.state