Question about class and instance

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

Question about class and instance

Joseph Alotta
Is there a general rule for class and instance?  

For example, if there should be only one instance of an object, should it be a class object?  

Or if I expect it to stay in memory and be available all the time, should it be a class object?

Thank you.




Reply | Threaded
Open this post in threaded view
|

Re: Question about class and instance

Chris Cunnington


On Fri, Oct 12, 2012 at 2:23 PM, Joseph J Alotta <[hidden email]> wrote:
Is there a general rule for class and instance?

For example, if there should be only one instance of an object, should it be a class object? 

Or if I expect it to stay in memory and be available all the time, should it be a class object?

Thank you.


There are answers to these questions, but the depend on the context. That's not an answer you want to hear. But here's a question you should ask: when do I need an instance and when do I need a class? Try this:

aClassOfYourChoice allInstances explore

highlight and do that. Take a class like:

Point allInstances explore

See what's going on in those instances. That'll start to show you the jobs of instances.
I don't know if that helps, but it came to mind. :)

Chris



Reply | Threaded
Open this post in threaded view
|

Re: Question about class and instance

Yanni Chiu
In reply to this post by Joseph Alotta
On 12/10/12 2:23 PM, Joseph J Alotta wrote:
> Is there a general rule for class and instance?
>
> For example, if there should be only one instance of an object, should it be a class object?

That is the Singleton pattern. Look for examples in the image - try
looking for senders/implementers of #default or #instance.

There's arguments on both sides, whether to use the class object itself
as the singleton, vs. using a single default instance. I think it comes
down to how likely it is that what you thought was a singleton, turns
out not to be, and then you have to move all the methods over from the
class-side to the instance-side.

> Or if I expect it to stay in memory and be available all the time, should it be a class object?

Your words "stay in memory" is a big concern. It indicates that you are
bringing some assumptions about programming from another programming
language/environment, to your learning of Smalltalk/Squeak. IMHO, that's
going to impair your ability to learn how to effectively learn and use
Smalltalk. Try to find articles/tutorials that explain the Smalltalk
image concept, and the concept of live-ness.


Reply | Threaded
Open this post in threaded view
|

Re: Question about class and instance

Igor Stasenko
On 12 October 2012 21:50, Yanni Chiu <[hidden email]> wrote:

> On 12/10/12 2:23 PM, Joseph J Alotta wrote:
>>
>> Is there a general rule for class and instance?
>>
>> For example, if there should be only one instance of an object, should it
>> be a class object?
>
>
> That is the Singleton pattern. Look for examples in the image - try looking
> for senders/implementers of #default or #instance.
>
> There's arguments on both sides, whether to use the class object itself as
> the singleton, vs. using a single default instance. I think it comes down to
> how likely it is that what you thought was a singleton, turns out not to be,
> and then you have to move all the methods over from the class-side to the
> instance-side.

... or when you want to limit the amount of messages your sole object
can understand.
With classes it is not that easy :)

>
>
>> Or if I expect it to stay in memory and be available all the time, should
>> it be a class object?
>
>
> Your words "stay in memory" is a big concern. It indicates that you are
> bringing some assumptions about programming from another programming
> language/environment, to your learning of Smalltalk/Squeak. IMHO, that's
> going to impair your ability to learn how to effectively learn and use
> Smalltalk. Try to find articles/tutorials that explain the Smalltalk image
> concept, and the concept of live-ness.
>

yes, i remember, at the beginning, when i just started using
smalltalk, i also had concerns about "all those objects staying in
memory,
and who knows what happens with them.. maybe something nasty ;)"


--
Best regards,
Igor Stasenko.