Simulator design question

david at aminal.com david at aminal.com
Fri Sep 7 17:19:23 UTC 2001


On Thu, Sep 06, 2001 at 03:09:32PM -0700, Ned Konz wrote:
> 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):
> 

Cool! I was groping my way towards using a dictionary, but the class variable 
didn't occur to me. The purple book stuff hasn't really sunk in yet, because
I'm sure this is in there, it just didn't click.

So, next I think I'm going to have to puzzle over whether the dictionary should
be in class Node, or if there should be a class Network where this dictionary
lives :-)

Thanks!

David Schutt


> 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