Simulator design question
Ned Konz
ned at bike-nomad.com
Thu Sep 6 22:09:32 UTC 2001
On Thursday 06 September 2001 03:38 pm, david at aminal.com wrote:
> So, I guess my question is , how do I make my Node instance known to
> the rest of the Smalltalk environment by its unique identifier? Is it
> possible to create global variables on-the-fly?
>
> In a workspace it seems so simple,
>
> uniqueID1 := Node new.
> uniqueID2 := Node new.
>
> etc.
>
> But I don't see how to do this in a 'new' method, where the variable name
> is generated and then pointed at the resulting instance.
The usual way to do this would be to have a class variable that is a
Dictionary of the mappings between names and instances. If you make this a
WeakValueDictionary, you don't have to worry about deleting members (Nodes is
a class variable):
Node class>>initialize
Nodes ifNil: [ Nodes := WeakValueDictionary new ].
So in Node new, you'd register the new instance with the Node class:
Node class>>new
| newNode |
newNode := super new initialize.
Nodes at: newNode id put: newNode.
Node class can then have a lookup method :
Node class>>named: anID
Nodes at: anID ifAbsent: [].
--
Ned Konz
currently: Stanwood, WA
email: ned at bike-nomad.com
homepage: http://bike-nomad.com
More information about the Squeak-dev
mailing list
|