



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Memon; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




1
Mar. 13, 2007
3
static boolean instance_flag = false;
condition.such non-functional classes and forgetting to check for this errorbecause there is nothing to keep you from creating many instances of
5
attempt to create an instancethe type of exception we must catch when weexception type so that the compiler will warn us ofHowever, it is convenient to have our own named
7
13
and keep track of instances.Create Singletons using a static method to issue
the classonly be created from within the static method ofmake the constructor private so an instance canTo prevent instantiating the class more than once,
15
return from the Instance methodthe singleton already exists-- you simply get a nullDon’t have to worry about exception handling if
17
private.because the constructor has been declared asclass directly, this will fail at compile timeshould you try to create instances of the iSpooler
public class
Singleton
{
//
Private
constructor suppresses generation
// of a
(public) default constructor
private
Singleton() {}
private
static class
SingletonHolder
{
private static
Singleton instance
= new
Singleton();
public}
static Singleton getInstance() {
return SingletonHolder.instance;
}
}
19
instances of the (previously) Singleton objectSuddenly, we decide that we can allow “n”