Thursday, January 31, 2013

Singleton Design Pattern

Posted by Rahul Kharde at 9:32 PM

"A class of which only a single instance can exist"
  • Singletons can be lazy loaded. Only when it is actually needed. That's very handy if the initialization includes expensive resource loading or database connections.
  • Singletons offer an actual object.
  • Singletons can be extended into a factory. The object management behind the scenes is abstract so it's better maintainable and results in better code.
You can use it to create a connection pool. It’s not wise to create a new connection every time a program needs to write something to a database; instead, a connection or a set of connections that are already a pool can be instantiated using the Singleton pattern.

For example,

public class Singleton {
   private static Singleton instance;  

   private Singleton(){}

   public static Singleton getInstance() {
     if (instance == null)
       instance = new Singleton();
     return instance;
   }
 }



If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top