In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to
coordinate actions across the system
There are only two
points in the definition of a singleton design pattern,
1.
there should be only one
instance allowed for a class and
2.
we should allow global
point of access to that single instance.
// "Singleton"
class Singleton
{
private static Singleton instance;
// Note: Constructor is 'protected'
protected Singleton()
{
}
public static Singleton Instance()
{
// Use 'Lazy initialization'
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
No comments:
Post a Comment