Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!newsfeed.internetmci.com!in2.uu.net!usc!sdd.hp.com!news.cs.indiana.edu!umn.edu!dialup-12-b-30
From: jhgreve@epx.cis.umn.edu (John Greve)
Subject: Re: Blocks on Disk
Message-ID: <DLoDqK.K1x@news.cis.umn.edu>
Sender: news@news.cis.umn.edu (Usenet News Administration)
Nntp-Posting-Host: dialup-10-a-14.gw.umn.edu
Organization: University of Minnesota, Twin Cities
References: <4e3obi$ue2@bocanews.bocaraton.ibm.com>
Date: Wed, 24 Jan 1996 06:55:20 GMT
Lines: 57

In article <4e3obi$ue2@bocanews.bocaraton.ibm.com>,
   sam@corvette.raleigh.ibm.com (Sam McHan) wrote:
>I have a tree of objects.  Within the tree, each node may need
>specialized behavior.  While walking the tree and visiting each
>node, the specialized methods for each node are executed.  If 
>specialized methods do not exist, then a default method is 
>executed.  Currently, the specialized code is held in an 
>instance variable by each node of the tree in a block.  Is there
>someway to store these blocks on disk (outside of the image) ?

>So... any ideas on how to do this?  Currently, the best idea I 
>have (which I don't consider to be much of an idea) is to store the
>source code string on disk and compile it as it is read in.  I would
>like to avoid using the compiler during runtime if possible.

Suggestion: change the problem.  Let the objects in the tree handle
the methods.  Or more precisely, let the nodes in the tree have custody
of an instance of something that implements the requisite behavior.

For example, you could create a Generalist class.  Let it (or its derived 
classes) worry about figuring out what the proper behavior is.
Now you can populate the tree with objects dereived from Generalist,
one instance at each node of the tree.  This way the base class Generalist
could implement default behavior.  Derived objects of Generalist (and
their derivations, etc^2) could alter that behavior as necessary.

Now you can add something like "#brodcast: #aMessage" to your Tree class.
class			TreeNode
superclass		???
instance variables	value, children

broadcast: aSymbol
	"value is supposed to that which holds a node's value"
	(self value) perform: aSymbol.
	(self children) do: [ :curNode| curNode broadcast: aMessage ].

And voila - now everything in the tree has responded to #aMessage (at least
to the best of its ability).

For persistance purposes you would need to add a store-to-stream
and load-from-stream method to Generalist.  This would need enough to
reconstitute an instance of the object in question (i.e. instance variables).
Let Smalltalk deal with the messy parts about storing blocks.  This will 
happen automatically because its already managing the methods for everything 
in the Object hieararchy.
   
If you need to have 2+ behaviors at any given node from the same message, 
well... maybe 1+ Generalists could live in a node.  But now we're talking a
hairy data structure.  I would urge you to rethink the design before going 
down this path.



   

John Greve
jhgreve@epx.cis.umn.edu
