[squeak-dev] singleton?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

[squeak-dev] singleton?

Joseph T. Bore

I would like to implement the following behavior, but im not sure how  
to accomplish it:

a method called plot, when its called, it would either create a new  
PlotMorph or if one is already on screen, re-use the existing plot  
morph.

I have the plot code working, but i cant figure out how to re-use the  
existing morph.

Im guessing a class side method that enforces a singleton via a class  
variable?  but when i destroy the morph, how do i have it clear out  
that variable so that subsequent calls will create a new one.

its very possible im coming at this entirely wrong, so feel free to  
make other suggestions for the implementation.

thanks in advance.

jb


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] singleton?

Tapple Gao
On Wed, Apr 09, 2008 at 11:41:27PM -0400, Joseph T. Bore wrote:
>
> I would like to implement the following behavior, but im not sure how to
> accomplish it:
>
> a method called plot, when its called, it would either create a new
> PlotMorph or if one is already on screen, re-use the existing plot morph.
>
> I have the plot code working, but i cant figure out how to re-use the
> existing morph.

(World findA: PlotMorph) ifNil: [PlotMorph new openInWorld; yourself]

--
Matthew Fulmer -- http://mtfulmer.wordpress.com/

Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Re: singleton?

Bernd Elkemann
> (World findA: PlotMorph) ifNil: [PlotMorph new openInWorld; yourself]
>
write this in a class-method named e.g. "theInstance".

But i prefer the following because it does not depend on World and does
not need a "find" (my guess is that it takes longer the more morphs are
in the World):
class-side method:
theInstance
^(self allInstances at: 1 ifAbsent: [self new].


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: singleton?

Bert Freudenberg

On 10.04.2008, at 11:00, Bernd Elkemann wrote:
>> (World findA: PlotMorph) ifNil: [PlotMorph new openInWorld; yourself]
> write this in a class-method named e.g. "theInstance".
>
> But i prefer the following because it does not depend on World and  
> does not need a "find" (my guess is that it takes longer the more  
> morphs are in the World):
> class-side method:
> theInstance
> ^(self allInstances at: 1 ifAbsent: [self new].


You cannot do this because it might find a PlotMorph in a different  
world (each project has a world).

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: singleton?

Tapple Gao
In reply to this post by Bernd Elkemann
On Thu, Apr 10, 2008 at 08:00:49PM +0200, Bernd Elkemann wrote:
>> (World findA: PlotMorph) ifNil: [PlotMorph new openInWorld; yourself]
> write this in a class-method named e.g. "theInstance".
>
> But i prefer the following because it does not depend on World and does not
> need a "find" (my guess is that it takes longer the more morphs are in the
> World):
> class-side method:
> theInstance
> ^(self allInstances at: 1 ifAbsent: [self new].

allInstances is much slower than World findA:;
- allInstances searches every object in memory
- findA: searches the "submorphs" ivar of the World

Also, do you really want to use a PlotMorph that is not visible
in your world?

--
Matthew Fulmer -- http://mtfulmer.wordpress.com/

Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: singleton?

Claus Kick
In reply to this post by Bert Freudenberg
Bert Freudenberg wrote:

>> class-side method:
>> theInstance
>> ^(self allInstances at: 1 ifAbsent: [self new].

> You cannot do this because it might find a PlotMorph in a different  
> world (each project has a world).

What about a Class Method with a Class Variable called Singleton:

singleton

Singleton ifNil:[Singleton := self new. ^Singleton].
^Singleton

Drawback is you have to remember to call MyClass singleton each time you
want to access the singleton, but well...

Claus


Reply | Threaded
Open this post in threaded view
|

Re: [squeak-dev] Re: singleton?

Herbert König
In reply to this post by Bernd Elkemann
Hello Bernd,


>> (World findA: PlotMorph) ifNil: [PlotMorph new openInWorld; yourself]
BE> But i prefer the following because it does not depend on World and does
BE> not need a "find" (my guess is that it takes longer the more morphs are
BE> in the World):
BE> class-side method:
BE> theInstance
BE> ^(self allInstances at: 1 ifAbsent: [self new].

my practical experience was just the opposite, I built a graph out of
Morphs on the srceen. Unaware of the problem Bert mentioned I used
GraphNode allInstances and GraphEdge allInstances.

It was quite slow in a bloated image. So I did World submorphs
detect: ... which was *much* faster.

Morph>>findA: basically does this and iterating over a collection of
only a small number is quite fast. (submorphs is just a collection.)

Whereas Behaviour>>allInstances has to dig through object after object
if it finds one of the appropriate class.

Which means in the end *every* object in the image has been scanned.
It doesn't help that this is done by primitives.

BTW PlotMorph allInstances will also give you all deleted but not
yet garbageCollected instances too. This might prevent creating a new
PlotMorph even if there is none left on the screen to display your
plot.

Cheers,

Herbert                                        


Reply | Threaded
Open this post in threaded view
|

[squeak-dev] Re: singleton?

Nicolas Cellier-3
Herbert König wrote:

> Hello Bernd,
>
>
>>> (World findA: PlotMorph) ifNil: [PlotMorph new openInWorld; yourself]
> BE> But i prefer the following because it does not depend on World and does
> BE> not need a "find" (my guess is that it takes longer the more morphs are
> BE> in the World):
> BE> class-side method:
> BE> theInstance
> BE> ^(self allInstances at: 1 ifAbsent: [self new].
>
> my practical experience was just the opposite, I built a graph out of
> Morphs on the srceen. Unaware of the problem Bert mentioned I used
> GraphNode allInstances and GraphEdge allInstances.
>
> It was quite slow in a bloated image. So I did World submorphs
> detect: ... which was *much* faster.
>
> Morph>>findA: basically does this and iterating over a collection of
> only a small number is quite fast. (submorphs is just a collection.)
>
> Whereas Behaviour>>allInstances has to dig through object after object
> if it finds one of the appropriate class.
>
> Which means in the end *every* object in the image has been scanned.
> It doesn't help that this is done by primitives.
>
> BTW PlotMorph allInstances will also give you all deleted but not
> yet garbageCollected instances too. This might prevent creating a new
> PlotMorph even if there is none left on the screen to display your
> plot.
>
> Cheers,
>
> Herbert                                        
>
>
>

As you only need the first, someInstance could reduce the cost a little
but would still be both inefficient and Project unfriendly.

Nicolas