Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Singleton class - instance or class variables?

Reply
Thread Tools

Singleton class - instance or class variables?

 
 
Caleb Tennis
Guest
Posts: n/a
 
      12-20-2005
When using a singleton class (one that includes Singleton, not an
eigenclass), can anyone think of any advantage/disadvantage to using
class variables over instance variables (or visa versa) to access
data in the class?

Thanks,
Caleb


 
Reply With Quote
 
 
 
 
Eero Saynatkari
Guest
Posts: n/a
 
      12-20-2005
Caleb Tennis wrote:
> When using a singleton class (one that includes Singleton, not an
> eigenclass), can anyone think of any advantage/disadvantage to using
> class variables over instance variables (or visa versa) to access
> data in the class?


I would recommend using class instance variables,
but the only reason I can think relates to resource
control and instance instantiation. For the typical
case one could just as well use classes and class
instance methods.

> Thanks,
> Caleb



E

--
Posted via http://www.ruby-forum.com/.


 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      12-20-2005
Caleb Tennis wrote:
> When using a singleton class (one that includes Singleton, not an
> eigenclass), can anyone think of any advantage/disadvantage to using
> class variables over instance variables (or visa versa) to access data
> in the class?


In answer to this question and the one about thread-specific singletons:
another approach to consider is using a dependency injection framework.
They all support singleton services, and at least some of them support
services that return one unique object per thread.

One disadvantage to using class variables (@@var) is that they are (in
the current ruby) per-hierarchy, not per class. So a subclass will share
the value of @@var. This is, I've heard, going to change in ruby 2.0.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


 
Reply With Quote
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      12-20-2005
On Tue, 20 Dec 2005, Caleb Tennis wrote:

> When using a singleton class (one that includes Singleton, not an
> eigenclass), can anyone think of any advantage/disadvantage to using class
> variables over instance variables (or visa versa) to access data in the
> class?


inheritence will break with either.

with @@var then

class Singleton
@@var = 42
end

class SingletonII < Singleton
@@var = 'forty-two'
end

class Singleton
p @@var #=> 'forty-two'
end

but with @var

class Singleton
@var = 42
end

class SingletonII < Singleton
end

class SingletonII
p @var #=> nil
end

my traits lib solves this - if inheritence of class data is important. it may
not be.

regards.

-a
--
================================================== =============================
| ara [dot] t [dot] howard [at] noaa [dot] gov
| all happiness comes from the desire for others to be happy. all misery
| comes from the desire for oneself to be happy.
| -- bodhicaryavatara
================================================== =============================



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton methods without the singleton class Charles Oliver Nutter Ruby 4 03-22-2010 10:46 PM
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM



Advertisments