473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Static Class or Singleton

Static and Singleton classes seem to provide very similar functionality from what I can see. Can someone tell me what the differences are and in what situation each would be better suited?

Steve
Apr 7 '10 #1
4 2106
Frinavale
9,735 Expert Mod 8TB
Singleton is a pattern that you can implement so that only one instance of the class is ever used.

It is a Static (Shared in VB.NET) class but it implements the Singleton Pattern.

A class that implements the Singleton Pattern does not have a public constructor (just a private one). It has a method that lets the calling code access the single instance of the class that exists (if it doesn't exist, this method will instantiate an instance of itself so that the calling class can get a reference to it).

See Wiki: Singleton Pattern...it does a better job of explaining than me :)

-Frinny
Apr 7 '10 #2
So is there any general rule of thumb to dictate when I should choose to use the Singleton design pattern over the Static class? And are there any performance differences between the two options?
Apr 8 '10 #3
@StevieMars
I think it's a matter of convenience and flexibility. With a static class, I think just about every attribute in the class needs to be static as well. I don't tend us use static classes, except for a place to store methods, so I'm not 100% on this.

With Singleton, you don't need to make every attribute static, so it may fit better with the rest of your classes, since it's really an object just like any other object, but only with the restriction that there can be only one instance.

"If" you discover that you need more than one instance, for load balancing or resource sharing or such, the Singleton can change into a Multiton quite easily; whereas, a static class cannot.

However, the singleton does have to be concerned with multiple threads, which static classes do not.
May 6 '10 #4
hype261
207 100+
Singletons are basically just a big global variable and if used incorrectly that can make for some difficult to track down bugs in your program. In my experience I used Singletons for logging information. One benefit of using a Singleton verses a Static class is that a Static class will always allocate memory where as a Singleton will only require memory to be allocated if it is used.
May 6 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
6
by: Kyle | last post by:
Hi, I have a library containing some global Variable. However, it seems that when the library is a static lib the initialization of the global var does not happen. I could not find any answer in...
7
by: Robert W. | last post by:
I've just been reading all about the Singleton class and understand how to implement and use it but I cannot understand why one NEEDS to use it instead of just declaring a class and implementing...
9
by: Joanna Carter \(TeamB\) | last post by:
Following on from the other discussion, I have to just check something out with reference to disposal of resources held in static fields. I have a Persistence Framework that is 'globally...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
7
by: John A Grandy | last post by:
For a singleton class utilizes by ASP.NET 2.0 page processing: When initial instantiation is performed during the initial call to the retrieve instance method (let's call the method...
4
by: DBC User | last post by:
I have a class with bunch of static methods. I could regourp all the static methods into seperate 3 or 4 classes. I was thinking about using Singlton pattern for all these 4 classes so that it...
9
by: wizwx | last post by:
There are two typical implementations of a singleton. The first one is to use a static pointer class Singleton { static Singleton * pSingleton; public: static Singleton * instance() {...
4
by: John Doe | last post by:
Hi, I have a singleton class defined like this : class UIManager : public CSingleton<UIManager>, public CObject { protected: DECLARE_DYNAMIC(UIManager) friend class CSingleton<UIManager>;
5
by: Andy B | last post by:
I have a class that I want to make static but it uses some objects that are instance objects. I keep getting a compiler error saying something about using instance objects in a static class or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.