0% found this document useful (0 votes)
136 views7 pages

Singleton Design Pattern Overview

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. It defines a getInstance() method that lets clients access its unique instance, which may be responsible for creating itself. For example, a print spooler class would use the Singleton pattern to ensure only one instance exists even though there are many printers.

Uploaded by

Utsav Khemka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views7 pages

Singleton Design Pattern Overview

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. It defines a getInstance() method that lets clients access its unique instance, which may be responsible for creating itself. For example, a print spooler class would use the Singleton pattern to ensure only one instance exists even though there are many printers.

Uploaded by

Utsav Khemka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Singleton

CS 124

Reference: Gamma et al
(“Gang-of-4”), Design Patterns
Singleton
 Intent
 Ensure a class has only one instance, and provide a global
point of access to it
 Motivation
 Important for some classes to have exactly one instance.
E.g., although there are many printers, should just have
one print spooler
 Ensure only one instance available and easily accessible
 global variables gives access, but doesn’t keep you from
instantiating many objects
 Give class responsibility for keeping track of its sole
instance
Design Solution
 Defines a getInstance() operation that lets
clients access its unique instance
 May be responsible for creating its own
unique instance

Singleton

-static uniqueinstance
Singleton data
… -Singleton()
return uniqueinstance; +static getInstance()
Singleton methods…
Singleton Example (Java)
 Database public class Database {
private static Database DB;
...
private Database() { ... }
Database public static Database getDB() {
if (DB == null)
static Database* DB DB = new Database();
return DB;
instance attributes…
}
...
static Database* getDB() }
instance methods…
In application code…
Database db = [Link]();
[Link]();
Singleton Example (C++)
class Database
{
private:
static Database *DB;
... In application code…
private Database() { ... } Database *db =
public: [Link]();
static Database *getDB() Db->someMethod();
{ if (DB == NULL)
DB = new Database());
return DB;
}
...
}
Database *Database::DB=NULL;
Implementation
 Declare all of class’s constructors private
 prevent other classes from directly creating an instance of
this class
 Hide the operation that creates the instance behind
a class operation (getInstance)
 Variation: Since creation policy is encapsulated in
getInstance, it is possible to vary the creation policy
Singleton Consequences
 Ensures only one (e.g., Database) instance exists in
the system
 Can maintain a pointer (need to create object on
first get call) or an actual object
 Can also use this pattern to control fixed multiple
instances
 Much better than the alternative: global variables

You might also like