AG VLSI Design and Architecture

SFB 501 - Project D1: Application System "Buildings"

PSiGene

Pattern Catalog

[PSiGene]

6.1 Pattern Singleton

Intent

Ensure a class only has one instance, and provide a global point of access to it.

Also Known As

Singleton [GoF]

Motivation

It's important for some classes to have exactly one instance. Although there can be many rooms in a building they all share the same weather.

How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but doesn't keep you from instantiating multiple objects.

A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and can provide a way to access the instance. This is the Singleton pattern.

Applicability

Use the Singleton pattern when

Structure

Participants

Objects:
Attributes:
Methods:

Consequences

The Singleton pattern has several benefits:

Implementation

{instance}
    {classVar} isNil ifTrue: [ {classVar} := {target} new. ].
    ^{classVar}
new
    {classVar} isNil ifTrue: [ {classVar} := super new ].
    ^{classVar}
finalize
    {classVar} := nil

Related Patterns

 

PEdit description


previous page next page up   Table of Contents PSiGene