No registered users in community xowiki
in last 10 minutes
in last 10 minutes
Re: [Xotcl] Help with singleton design pattern
From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Tue, 22 Apr 2003 12:03:56 +0200
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
Date: Tue, 22 Apr 2003 12:03:56 +0200
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