Dofactory.com
Dofactory.com
 Back to list
Views:   16.9K
Replies:  3
Archived

Singleton Design Pattern

Hello,

I have a question about the Singleton design pattern.

Is Singleton pattern about maintaining one instance of a class or is it about maintaining one state of a class. What I can think of is this: if it's about creating one instance of a class then static classes are not a way of implementing Singleton design pattern. But if it's about maintaining one state of a class then there is nothing like monostate pattern.

Please advice.
Regards.
Ram Pandey, May 16, 2010
Reply 1

Go through this link, you will have better understanding of singleton pattern

http://en.wikipedia.org/wiki/Singleton_pattern#C.23
Bhupendra Sinha, May 21, 2010
Reply 2
Some differences between Singletons and Static classes are...

* Singletons can implement interfaces

* You cannot pass a static class as a parameter to a method, this can be done with a Singleton

* A Singleton is a normal class that is instantiated just once, a static class is never instantiated


UPDATE
As a side note, I usually tend not to use either of these. Instead I use my DI container (Ninject, StructureMap, ...) to supply me with a singleton of the required type when needed.
Robert Blixt, May 17, 2010
Reply 3
Singleton Pattern is a creational pattern and is all about making sure we have only one instance (maximum) of a class at any given point in time.

It just needs a private constructor so that none of the external instances can directly create an instance of the singleton class under consideration. This Singleton class needs to expose one public method eg: GetInstance, which intenally calls the private constuctor only if there is no preexisting instance of the class. Lock statements can be used in the implementation to avoid any hassles of multi-threaded access to the Singleton.

Sreenivas Manyam Rajaram, May 16, 2010
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.