Welcome to Mike95.com
Home
WELCOME
ABOUT MIKE95.COM
CONTACT ME


Features
FUNNY JOKES
HUMAN FACTOR


C++
Library Source Code:
CGIPARSE
JSTRING
JVECTOR
JSTACK
JHASHTABLE


COM / ASP
MIKE95 COM SVR
- ASP STACK
- ASP QUEUE
- ASP VECTOR


Tutorials:
TEMPLATES
ALGORITHMS
DESIGN PATTERNS


Sample Work
Internet Based
TASK/BUG TRACKER


Visual C++ / MFC
FLIGHT PATH (C++)
MP3 PLAY LIST


Java
JAVA TALK
BAR GRAPH
WEB CAM


Personal
CONTACT ME
RESUME
PICTURES
TRIPS
Design Patterns
Singleton Pattern

Many projects involve numerous inter-dependent components each with their own set of functions and responsibilities.  In most applications, there is usually a need for a globally accessible object for storing global information.  For example, login credentials for an application can be such a case.  Your application can grow to many objects (perhaps thousands), however you will always have a need to check if the user is logged in or not in a simple and convenient way without affecting the local functionality of the object you are working with.  Additionally, there should only be a single instance of the login credentials object since duplicating it may create inconsistencies should theuser logoff abrumptly and only the original instance is updated as such.

Here, a simple application of the Single pattern can save the day.  A singleton is a globally accessible single instance object.

To apply the singleton pattern, as the name implies, you create a class Authenticate and you disallow explicit construction of the class.  Additionally, you provide a property to always obtain the single instance always in existance.  The following code demonstrates a Login authentication class.

public class Authenticate
{
	//reference to single instance
	protected static Authenticate _Authenticate; 
	
	static Authenticate()
	{
		//create the only instance
		_Authenticate = new Authenticate();
	}
	
	public static Authenticate Current
	{
		get	{ return _Authenticate; }
	}
	
	private Authenticate() { } //do not allow explicit construction
}

(c)2024 Mike95.com / Site Disclaimer
Site Meter