CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2001 Singleton |
||
---|---|---|
© 2001, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 27-Mar-01 |
Singleton
Intent
Insure a class only has one instance, and provide a global point of access to it
Motivation
There are times when a class can only have one instance
Applicability
Use the Singleton pattern when
Examples of Using a Singleton
Java Security manager All parts of a program must access the same security manager
Once set a security manager cannot be changed in a program
Logging the activity of a server All parts of the server should use the same instance of the logging system
The server should not be able to change the instance of the logging system was it has been set
Null Object
Null object does not have state, so only need one instance
A binary tree with N non-null nodes will contain N+1 NullNodes
Command Processor
If Command Processor is to control all commands:
Implementation
Java
// Only one object of this class can be created class Singleton { private static Singleton _instance = null; private Singleton() { fill in the blank } public static Singleton getInstance() { if ( _instance == null ) _instance = new Singleton(); return _instance; } public void otherOperations() { blank; } } class Program { public void aMethod() { X = Singleton.getInstance(); } }
Java Singletons, Classes, Garbage Collection
Classes can be garbage collected in Java
Only happens when there are
Implementation C++
// Only one object of this class can be created class Singleton { private: static Singleton* _instance; void otherOperations(); protected: Singleton(); public: static Singleton* getInstance(); } Singleton* Singleton::_instance = 0; Singleton* Singleton::getInstance() { if ( _instance == 0 ) _instance = new Singleton; return _instance; }
Implementation Smalltalk
Object subclass: #Singleton instanceVariableNames: '' classVariableNames: 'UniqueInstance ' poolDictionaries: '' category: 'Whitney-Examples' current UniqueInstance ifNil: [UniqueInstance := Singleton basicNew]. ^UniqueInstance new self error: 'Use current to get an instance of Class ' , self name
Overriding new in Smalltalk
Since can control what new returns one might be tempted to use:
new UniqueInstance ifNil: [UniqueInstance := Singleton basicNew]. ^UniqueInstance
This can be misleading, user might think they are getting different objects when calling new
Do we have two different windows below or not?
| left right | left := SingleWindow new. Right := SingleWindow new. left position: 100@ 100. right position: 500@100.
Naming the Access Method
GOF uses: instance()
POSA 1 uses: getInstance()
Smalltalk uses default and current
Selecting names is one of the more difficult problems in object-oriented analysis. No name is perfect [1]
Singletons and Static
If one needs only one instance of a class why not just implement all methods as static?
Consequences
[1] Fowler pp. 9, Alpert pp. 98
Copyright ©, All rights reserved.
2001 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous    visitors since 27-Mar-01    Next