Index: doc/example-scripts/rosetta-singleton.html =================================================================== diff -u -r24cb8f4bffd49c9375c1c64aa0610933b62511bb -rc4f449cb353be812ba6502ef8e9587e87881f59b --- doc/example-scripts/rosetta-singleton.html (.../rosetta-singleton.html) (revision 24cb8f4bffd49c9375c1c64aa0610933b62511bb) +++ doc/example-scripts/rosetta-singleton.html (.../rosetta-singleton.html) (revision c4f449cb353be812ba6502ef8e9587e87881f59b) @@ -1,858 +1,858 @@ - - - - - -Listing of doc/example-scripts/rosetta-singleton.tcl - - - - - -
-
-

Rosetta Example: Singleton

-
- -
-

A Singleton Class

-
-
-
package req nx
-
-nx::Class create Singleton {
-  #
-  # We overload the system method "create". In the modified method we
-  # save the created instance in the instance variable named
-  # "instance"
-  #
-  :variable instance:object
-
-  :public object method create {args} {
-    return [expr {[info exists :instance] ? ${:instance} : [set :instance [next]]}]
-  }
-}
-
-
-

Demonstrating the behavior in a shell:

-

Calling Singleton new multiple times returns always the same object:

-
-
-
% expr {[Singleton new] eq [Singleton new]}
-1
-
-
-

A Singleton Meta-class

-

Alternatively, we can follow a more generic approach and define a -metaclass which allows to define several application classes as -singletons. The metaclass has the most general metaclass nx::Class -as superclass. In contrary to the example obove, the create method -is not defined as a class method, but it will be inherited to its -instances (to the application classes).

-
-
-
nx::Class create Singleton -superclass nx::Class {
-  #
-  # We overload the system method "create". In the modified method we
-  # save the created instance in the instance variable named
-  # "instance"
-  #
-  :variable instance:object
-
-  :public method create {args} {
-    return [expr {[info exists :instance] ? ${:instance} : [set :instance [next]]}]
-  }
-}
-

Create an application class named Counter as a singleton:

-
-
-
% Singleton create Counter
-::Counter
-

Calling Counter new multiple times returns always the same object:

-
-
-
% expr {[Counter new] eq [Counter new]}
-1
-
-
-
-
-

- - - + + + + + +Listing of doc/example-scripts/rosetta-singleton.tcl + + + + + +
+
+

Rosetta Example: Singleton

+
+ +
+

A Singleton Class

+
+
+
package req nx
+
+nx::Class create Singleton {
+  #
+  # We overload the system method "create". In the modified method we
+  # save the created instance in the instance variable named
+  # "instance"
+  #
+  :variable instance:object
+
+  :public object method create {args} {
+    return [expr {[info exists :instance] ? ${:instance} : [set :instance [next]]}]
+  }
+}
+
+
+

Demonstrating the behavior in a shell:

+

Calling Singleton new multiple times returns always the same object:

+
+
+
% expr {[Singleton new] eq [Singleton new]}
+1
+
+
+

A Singleton Meta-class

+

Alternatively, we can follow a more generic approach and define a +metaclass which allows to define several application classes as +singletons. The metaclass has the most general metaclass nx::Class +as superclass. In contrary to the example obove, the create method +is not defined as a class method, but it will be inherited to its +instances (to the application classes).

+
+
+
nx::Class create Singleton -superclass nx::Class {
+  #
+  # We overload the system method "create". In the modified method we
+  # save the created instance in the instance variable named
+  # "instance"
+  #
+  :variable instance:object
+
+  :public method create {args} {
+    return [expr {[info exists :instance] ? ${:instance} : [set :instance [next]]}]
+  }
+}
+

Create an application class named Counter as a singleton:

+
+
+
% Singleton create Counter
+::Counter
+

Calling Counter new multiple times returns always the same object:

+
+
+
% expr {[Counter new] eq [Counter new]}
+1
+
+
+
+
+

+ + +