Sunday, April 12, 2009

Usage of Signleton and Static

Hi All,

In my professional life I have seen people who use singleton and static classes with such a lousiness and without having any clear concepts about it.

So I thought I should comeup with the ideas which could become the guidelines to the developer(s) about using them properly and efficiently.

When you want to do some action or you need to develop some utitlity classes, always try to use "static" because it will not create any instance and no memory allocation will happen.

When you want to develop application which need single instance, or you want to initialize some data which can be used over the whole life cycle of the application, then use "Singleton".

Normally, the "Entry Point" of the application are made "Singleton" as its instance remain the same over the lifecycle of the application.

One more example is when we need to cache some data, we can use both static and singleton. However, the best way is to use "Singleton" as we can control the instance.

And about memory consumption, yes ofcourse static is faster than singleton but the difference is so minor which could be easily ignored.

Cheers and regards



No comments: