Singleton Pattern - Programming Language Technologies and Paradigms | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Professor: Memon; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-jcf
koofers-user-jcf 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Singleton Pattern
Mar. 13, 2007
2
What is it?
If you need to make sure that there can be one and
only one instance of a class.
For example, your system can have only one window
manager or print spooler, or a singl e point of access to a
database engi ne.
3
Approach 1
Embed a static variable inside the class that we set
on the first instance and check for each time we
enter the constructor.
A static variable is one for which there is on ly one
instance, no matter how many instances there are of the
class.
static boolean instance_flag = false;
4
Disadvantage of Approach 1
How to find out whether creating an instance was
successful or not
Remember constructors do not return values.
One way would be to call a method that checks for
the success of creation, and which simply returns
some value derived from the static variable.
This is inelegant and prone to error
because there is nothing to keep you from creating many instances of
such non-functional classes and forgetting to check for this error
conditio n.
pf3
pf4
pf5

Partial preview of the text

Download Singleton Pattern - Programming Language Technologies and Paradigms | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Singleton PatternSpring 2007

Mar. 13, 2007

What is it?

  • For example, your system can have only one windowonly one instance of a class.If you need to make sure that there can be one and database engine.manager or print spooler, or a single point of access to a

3

Approach 1

  • A static variable is one for which there is only oneenter the constructor.on the first instance and check for each time weEmbed a static variable inside the class that we set class.instance, no matter how many instances there are of the

static boolean instance_flag = false;

Disadvantage of Approach 1

  • Remember constructors do not return values.successful or notHow to find out whether creating an instance was
  • This is inelegant and prone to errorsome value derived from the static variable.the success of creation, and which simply returnsOne way would be to call a method that checks for

condition.such non-functional classes and forgetting to check for this errorbecause there is nothing to keep you from creating many instances of

5

Approach 2

  • Let’s create our own exception class for this caseinstantiated more than once.Create a class that throws an Exception when it is

Why a New Exception?

  • other than calling its parent classes through the super()particularThis new exception type doesn’t do anything in method,.

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

Lets Implement the Class

Lets Use it!

13

Approach 4

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,

Approach 4

15

Approach 4

return from the Instance methodthe singleton already exists-- you simply get a nullDon’t have to worry about exception handling if

Lets Use it!

17

Compile-time Error Checking

private.because the constructor has been declared asclass directly, this will fail at compile timeshould you try to create instances of the iSpooler

Approach 5

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

Dropping the Singleton Requirement

instances of the (previously) Singleton objectSuddenly, we decide that we can allow “n”

  • Which implementation is “maintainable”?approaches?How would you adapt each of the previous five