Total Pageviews

Tuesday, September 11, 2012

Static Class and Singleton Pattern



Static classes and singletons both provide sharing of redundant objects in memory, but they are very different in usage and implementation.

Static Class - You can not create the instance of static class.
Singleton pattern - you can create one instance of the object and reuse it.
Static classes- are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Singleton instance is created for the first time when the user requested.
Static Class class cannot have constructor.
singleton class can have constructor.


++++++++++++++++++
static members can operate only on static data and call static methods of the
defining class. If you attempt to make use of nonstatic class data or call a nonstatic method of the class within a
static member’s implementation, you’ll receive compile-time errors.Singleton has a private constructor
Use singleton as parameter.

Static Class - You can not create the instance of static class.
Singleton pattern - you can create one instance of the object and reuse it.
Static classes- are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Singleton instance is created for the first time when the user requested.
Static Class class cannot have constructor.
singleton class can have constructor.

http://www.dotnetspider.com/forum/145836-Difference-between-Static-class-Singleton-Instance.aspx


Introducing the Singleton Pattern
Our ideal solution is called the singleton design pattern. Here is an implementation of a singleton that I will use. As you know, a singleton is a single-instance object. It is highly efficient and very graceful. Singletons have a static property that you must access to get the object reference.



Here we looked at some aspects of the singleton design pattern in the C# language. Singletons allow you to reuse code and control object state much easier. This improves code-sharing, and can result in a far cleaner body of code. With less code, your programs will usually have fewer bugs and will be easier to maintain.

1 comment: