[Xotcl] Help with singleton design pattern

Gustaf Neumann neumann at wu-wien.ac.at
Tue Apr 22 12:03:56 CEST 2003


On Saturday 19 April 2003 07:40, Michael A. Cleverly wrote:
> As I'm starting to learn XOTcl, I decided to try and implement a singleton
> design pattern as an educational exercise.

 Dear Michael,

 Uwe has already answered the filter-part. To solve the singleton
 problem, there is much less code necessary. The following example
 uses a metaclass that provides an instproc "new":

==========================================
Class Singleton -superclass Class
Singleton instproc new args {
  expr {[my exists instance] ? 
	[my set instance] : [my set instance [next]]}
}
==========================================

 A new singleton can be created in a declarative way, and
 all calls to "new" will return the same instance (you can 
 also raise errors, if you prefer)

==========================================
Singleton MyClass
puts "1. [MyClass new]"
puts "2. [MyClass new]"
==========================================

 Certainly the same maechanism can be applied on "create" as well.
 In the current XOTcl version, you have to duplicate the instproc 
 for "create", in the forthcoming xotcl version registering a single
 instproc on create will be sufficient (in 1.0.2, the method new
 calls directly the implementation of create, the next version
 will go through the dispatcher).

 best regards
-gn

-- 
Univ.Prof. Dr.Gustaf Neumann
Abteilung für Wirtschaftsinformatik
WU-Wien, Augasse 2-6, 1090 Wien



More information about the Xotcl mailing list