[Prev][Next][Index][Thread]

Re: Representing percentage



On Wed, 31 Jan 2001 11:30:02 -0500 (EST), "Nimi" <nimi@better.tv> wrote:

> 
> "Scott McKay" <swm@mediaone.net> wrote in message
> hrWc6.21786$t3.4383959@typhoon.ne.mediaone.net">news:hrWc6.21786$t3.4383959@typhoon.ne.mediaone.net...
> > So -- even in a language like Cecil which supports this kind
> > of thing -- you want to either represent percentages as real,
> > live classes, or you should just interpret an integer properly
> > in the right place.  Note that there's no possibility that you
> > will have more than 100 different percentages, right?  If
> > you make <percentage> be immutable (the setter for p
> > being #f), you can add a 'make' method that chooses from
> > 1 of the 101 possible percentage objects, and then share
> > among them.  (I think the design pattern weenies would
> > say they are using a flyweight coupled with some sort of
> > instance factory...)  This is a more portable solution than
> > the admittedly cool Gwydion hack.
> 
> I like Neel's trick (thanks Neel) but it's important for me to keep the code
> portable. I was toying with the instance factory option that you suggested
> and bumped into another question - I thought of adding a class-allocated
> slot to the <percentage> to hold all instances of <percentage>, and override
> make() to either return an instance from that slot or create a new one,
> but - how can I access a class-allocated slot if I don't have a reference to
> any of its instances (e.g. from the make() override)? I could use a module
> variable instead of the class-allocated slot but it won't be the same.
> 

This is an FAQ. Have a look through dejanews.

Basically, people have suggested a new API for accessing class-allocated
slots without an instance. Some others have argued that this violates a
nice property of classes and instances.

Some specific answers for you are:

(0) make an instance to query on the fly
(1) put a canonical instance in a module variable
(2) put the list directly into a module variable

You can always create an interface that makes it look as if you are
accessing the class by specializing on a singleton:

define constant *percentages* = make(<table>);

define method percentages ( x == <percentage> )
  *percentages*
end;

__Jason


References: