Skip to content Skip to navigation

Connexions

You are here: Home » Content » Singleton Design Pattern

Navigation

Lenses

What is a lens?

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

This content is ...

In these lenses

  • Design Patterns display tagshide tags

    This module is included inLens: Design Patterns
    By: Stephen Wong

    Comments:

    "Enables only a single instance of a class to exist."

    Click the "Design Patterns" link to see all content selected in this lens.

    Click the tag icon tag icon to display tags associated with this content.

Recently Viewed

This feature requires Javascript to be enabled.

Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.
 

Singleton Design Pattern

Module by: Stephen Wong. E-mail the author

Summary: Singletons model situations where only a single instance of a class can exist in a system.

The Singleton design pattern is used to represent an object in a situation where only a single instance of that object can exist. Situations where this might arise include objects representing null or empty, centralized broker objects, centralized accumulator objects, etc.

There are two possible techniques for implementing Singletons in object-oriented systems. Both techniques are illustrated in the diagram below:

Below, the class Singleton is the class for which only a single instantiation is desired. Both techniques rely on using a private constructor to prevent clients from creating more instantiations of Singleton. The single instance is held as the static class variable, ONLY.

Figure 1: A Singleton can be implemented by either exposing a public, static, final field or accessor method.
Two Examples of a Singleton Implementation
Singleton examples

The public static field technique (at the top of the diagram) can be implemented in Java but not in C++ because Java is capable of automatically initializing class members while C++ cannot. In this technique, the client accesses the solitary instance via the public, static class member Singleton.ONLY. This field is accessible by any object that has access to the Singleton class.

The public static method technique (at the bottom of the diagram) can be implemented in any language. Here, the client obtains a reference to the sole instance by calling the public, static class method Singleton.getONLY(). This method is accessible by any object that has access to the Singleton class. The static class field ONLYis not automatically initialized. Instead, its initialization occurs the first time that Singleton.getONLY() is called by the client.

The static field technique simpler, if it can be implemented in the language of choice. However, the static method technique has an advantage when the Singleton class is large, expensive and/or time-intensive to instantiate. Initialization of the singleton instance only occurs if and when it is needed in the this technique.

Content actions

Download module as:

PDF | EPUB (?)

What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

Downloading to a reading device

For detailed instructions on how to download this content's EPUB to your specific device, click the "(?)" link.

| More downloads ...

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks