Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!bb3.andrew.cmu.edu!newsfeed.pitt.edu!gatech!newsxfer2.itd.umich.edu!newsxfer.itd.umich.edu!qiclab.scn.rain.com!slc.com!servio!servio!aland
From: aland@servio.slc.com (Alan Darlington)
Subject: Re: Too many ivars! Can a design pattern help??
Message-ID: <1996Feb26.221703.26668@slc.com>
Sender: news@slc.com (USENET News)
Nntp-Posting-Host: servio
Organization: GemStone Systems, Inc., Beaverton OR, USA
References: <312CABDC.3FE@stn14.me.calpoly.edu>
Date: Mon, 26 Feb 1996 22:17:03 GMT
Lines: 29

"Mark S. Johnson" <mjohnson@stn14.me.calpoly.edu> writes:
<snip>
> My "problem" is that I have _a lot_ of instance variables (20+).
> How did I get in this mess?  Two reasons:
<snip> 
> Is there a pattern or process anyone can recommend to me as I
> refactor/redesign this object?

Instead of using instant variables for caching computed values,
why not use a dictionary for the cache, and some symbol for the
key.  For example,

  someCalc
    | oldValue value |
    oldValue := self cachedNumbers
      at: #myKey
      ifAbsent: [nil].
    oldValue notNil ifTrue:
      [^ oldValue].
    " your calculation here "
    self cachedNumber
      at: #myKey
      put: value.   " what your result is "
    ^ value

  Hope this helps,
  Alan
    (standard disclaimer)

