FACTOID # 126: Iceland has many, many more tractors per 1000 hectares of cropland than any other nation - more than twice that of the next highest country, Slovenia.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


EncyclopediaSingleton pattern > ActionScript 3 Example Implementation

In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an anti-pattern since it is often used as a euphemism for global variable. Software engineering (SE) is the application of a systematic, disciplined, quantifiable approach to the development, operation, and maintenance of software. ... In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. ... Instantiation is the process of creating a specific object (computing) which is a member or instance of a class (computing). ... Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. ... Anti-patterns, comprises the study or specific repeated practices that appear initially to be beneficial, but ultimately result in bad consequences that outweigh the hoped-for advantages. ... Euphemism is the substitution of an agreeable or inoffensive expression for one that may offend or suggest something unpleasant to the listener; or in the case of doublespeak, to make it less troublesome for the speaker. ... In computer programming, a global variable is a variable that is accessible in every scope. ...

Contents

Common uses

  • The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation.
  • Façade objects are often Singletons because only one Façade object is required.
  • State objects are often Singletons.
  • Singletons are often preferred to global variables because:
    • They don't pollute the global namespace (or, in languages with namespaces, their containing namespace) with unnecessary variables.[1]
    • They permit lazy allocation and initialization, where global variables in many languages will always consume resources.
  • Singletons behave differently depending on the lifetime of the virtual machine. While a software development kit may start a new virtual machine for every run which results in a new instance of the singleton being created, calls to a singleton e.g. within the virtual machine of an application server behave differently. There the virtual machine remains alive, therefore the instance of the singleton remains as well. Running the code again therefore can retrieve the "old" instance of the singleton which then may be contaminated with values in local fields which are the result of the first run.

A software design pattern, the Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme. ... The Builder Pattern is a software design pattern. ... A prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. ... In computer programming, lazy evaluation is a technique that attempts to delay computation of expressions until the results of the computation are known to be needed. ...

Class diagram

Image File history File links No higher resolution available. ...

Implementation

The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made either private or protected. Note the distinction between a simple static instance of a class and a singleton: although a singleton can be implemented as a static instance, it can also be lazily constructed, requiring no memory or resources until needed. Another notable difference is that static member classes cannot implement an interface, unless that interface is simply a marker. So if the class has to realize a contract expressed by an interface, you really have to make it a singleton. In object-oriented programming, a class is a programming language construct used to group related fields and methods. ... In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special block of statements called when an object is created, either when it is declared (statically constructed on the stack, possible in C++ but not in Java and other object-oriented languages) or dynamically constructed...


The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one. If the programming language has concurrent processing capabilities the method should be constructed to execute as a mutually exclusive operation. Many programming languages, operating systems, and other software development environments support what are called threads of execution. ...


The classic solution to this problem is to use mutual exclusion on the class that indicates that the object is being instantiated. Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the simultaneous use of un-shareable resources by pieces of computer code called critical sections. ...


Example implementations

Scala

The Scala programming language supports Singleton objects out-of-the-box. The 'object' keyword creates a class and also defines a singleton object of that type. Scala is a multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. ...

 object Example extends String { // creates a singleton called Example } 

Java

The Java programming language solutions provided here are all thread-safe but differ in supported language versions and lazy-loading. Java language redirects here. ... Thread-safety is a computer programming concept applicable to multi-threaded programs. ... In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. ...


The solution of Bill Pugh

This is the recommended method. It is known as the initialization on demand holder idiom and is as lazy as possible. Moreover, it works in all known versions of Java. This solution is the most portable across different Java compilers and virtual machines. William Pugh (Bill Pugh) is the inventor of the Skip List. ... In software engineering, the Initialization on Demand Holder idiom (design pattern) is a lazy-loaded singleton. ...


The inner class is referenced no earlier (and therefore loaded no earlier by the class loader) than the moment that getInstance() is called. Thus, this solution is thread-safe without requiring special language constructs (i.e. volatile and/or synchronized). Thread-safety is a computer programming concept applicable to multi-threaded programs. ...

 public class Singleton { // Private constructor suppresses generation of a (public) default constructor private Singleton() {} /** * SingletonHolder is loaded on the first execution of Singleton.getInstance() * or the first access to SingletonHolder.instance , not before. */ private static class SingletonHolder { private final static Singleton instance = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.instance; } } 

Traditional simple way

Just like the one above, this solution is thread-safe without requiring special language constructs, but it lacks the laziness. The INSTANCE is created as soon as the Singleton class loads. That might even be long before getInstance() is called. It might be (for example) when some static method of the class is used. If laziness doesn't bother you or you need the instance to be created early in your application's execution, you can use this (slightly) simpler solution: Thread-safety is a computer programming concept applicable to multi-threaded programs. ...

 public class Singleton { private final static Singleton instance = new Singleton(); // Private constructor suppresses generation of a (public) default constructor private Singleton() {} public static Singleton getInstance() { return instance; } } 

Java 5 solution

If and only if your compiler is Java 5 (also known as Java 1.5) or newer, AND all Java virtual machines your application is going to run on fully support the Java 5 memory model, then (and only then) you can use volatile double checked locking (for a detailed discussion of why you should never do this before Java 5 see The "Double-Checked Locking is Broken" Declaration):

 public class Singleton { private static volatile Singleton INSTANCE; // Private constructor suppresses generation of a (public) default constructor private Singleton() {} public static Singleton getInstance() { if (INSTANCE == null) synchronized(Singleton.class) { if (INSTANCE == null) INSTANCE = new Singleton(); } return INSTANCE; } } 

Allen Holub (in "Taming Java Threads", Berkeley, CA: Apress, 2000, pp. 176-178) notes that on multi-CPU systems (which are widespread as of 2007), the use of volatile may have an impact on performance approaching to that of synchronization, and raises the possibility of other problems. Thus this solution has little to recommend it over Pugh's solution described above.


PHP 5

Singleton pattern in PHP 5:

 <?php class Singleton { // object instance private static $instance; private function __construct() {} public static function getInstance() { if (self::$instance === null) self::$instance = new self; return self::$instance; } public function doAction() { ... } } //usage Singleton::getInstance()->doAction(); ?> 

Actionscript 3.0

In Actionscript 3.0 class constructors have to be public. Here is one solution for Actionscript 3.0:

 package { public final class Singleton { /** * Storage for the singleton instance. */ private static const instance:Singleton = new Singleton ( SingletonLock ); /** * Constructor * * @param lock The Singleton lock class to pevent outside instantiation. */ public function Singleton(lock:Class) { // Verify that the lock is the correct class reference. if ( lock != SingletonLock ) { throw new Error( "Invalid Singleton access. Use Singleton.getInstance()" ); } } public static function getInstance():Singleton { return Singleton.instance; } } } /** * This is a private class declared outside of the package * that is only accessible to classes inside of the Singleton.as * file. Because of that, no outside code is able to get a * reference to this class to pass to the constructor, which * enables us to prevent outside instantiation. */ class SingletonLock { } 

And here is another solution, without using the private class outside package:

 package { public final class Singleton { private static const instance:Singleton = new Singleton(); public function Singleton() { if( instance ) throw new Error( "This class is a singleton and can only be accessed through Singleton.getInstance()" ); } public static function getInstance():Singleton { return instance; } } } 

Objective-C

A common way to implement a singleton in Objective-C is the following: Objective-C, often referred to as ObjC or more seldomly as Objective C or Obj-C, is an object oriented programming language implemented as an extension to C. It is used primarily on Mac OS X and GNUstep, two environments based on the OpenStep standard, and is the primary language...

 @interface MySingleton : NSObject { } + (MySingleton *)sharedSingleton; @end @implementation MySingleton + (MySingleton *)sharedSingleton { static MySingleton *sharedSingleton; @synchronized(self) { if (!sharedSingleton) sharedSingleton = [[MySingleton alloc] init]; return sharedSingleton; } } @end 

If thread-safety is not required, the synchronization can be left out, leaving the +sharedSingleton method like this:

 + (MySingleton *)sharedSingleton { static MySingleton *sharedSingleton; if (!sharedSingleton) sharedSingleton = [[MySingleton alloc] init]; return sharedSingleton; } 

This pattern is widely used in the Cocoa frameworks (see for instance, NSApplication, NSColorPanel, NSFontPanel or NSWorkspace, to name but a few). A Cocoa application being developed using Xcode. ...


Some may argue that this is not, strictly speaking, a Singleton, because it is possible to allocate more than one instance of the object. A common way around this is to use assertions or exceptions to prevent this double allocation.

 @interface MySingleton : NSObject { } + (MySingleton *)sharedSingleton; @end @implementation MySingleton static MySingleton *sharedSingleton; + (MySingleton *)sharedSingleton { @synchronized(self) { if (!sharedSingleton) [[MySingleton alloc] init]; return sharedSingleton; } } +(id)alloc { @synchronized(self) { NSAssert(sharedSingleton != nil, @"Attempted to allocate a second instance of a singleton."); sharedSingleton = [super alloc]; return sharedSingleton; } } @end 

There are alternative ways to express the Singleton pattern in Objective-C, but they are not always as simple or as easily understood, not least because they may rely on the -init method returning an object other than self. Some of the Cocoa "Class Clusters" (e.g. NSString, NSNumber) are known to exhibit this type of behaviour. A Cocoa application being developed using Xcode. ...


Note that @synchronized is not available in some Objective-C configurations, as it relies on the NeXT/Apple runtime. It is also comparatively slow, because it has to look up the lock based on the object in parentheses. Check the history of this page for a different implementation using an NSConditionLock.


C++

Here is a possible implementation in C++, using the Curiously Recurring Template Pattern. In this implementation, also known as the Meyers singleton [2], the singleton is a static local object. Because C++ provides no standard multithreading support, this solution is not thread-safe in general, though some compilers (e.g. gcc) generate thread-safe code in this case. C++ (pronounced see plus plus, IPA: ) is a general-purpose programming language with high-level and low-level capabilities. ... In software engineering, the curiously recurring template pattern (CRTP), is a software design pattern where a base class template is instantiated with a derived class type as its template parameter. ... The GNU Compiler Collection (usually shortened to GCC) is a set of programming language compilers produced by the GNU Project. ...

 #include <iostream> template<typename T> class Singleton { public: static T& Instance() { static T theSingleInstance; // assumes T has a protected default constructor return theSingleInstance; } }; class OnlyOne : public Singleton<OnlyOne> { friend class Singleton<OnlyOne>; int example_data; public: int Getexample_data() const {return example_data;} protected: OnlyOne(): example_data(42) {} // default constructor  }; #define ReferenceName OnlyOne::Instance() /* This test case should print "42". */ #include <iostream> int main() { std::cout<< ReferenceName.Getexample_data()<<std::endl; return 0; } 

C++ (using pthreads)

A common design pattern for thread safety with the singleton class is to use double-checked locking. However, due to the ability of modern processors to re-order instructions (as long as the result is consistent with their architecturally-specified memory model), and the absence of any consideration being given to multiple threads of execution in the language standard, double-checked locking is intrinsically prone to failure in C++. There is no model — other than runtime libraries (e.g. POSIX threads, designed to provide concurrency primitives) — that can provide the necessary execution order.[1] A design pattern is a formal way of documenting successful solutions to problems. ... Thread-safety is a computer programming concept applicable to multi-threaded programs. ... In software engineering, double-checked locking is a software design pattern also known as double-checked locking optimization[1]. The pattern is designed to reduce the overhead of acquiring a lock by first testing the locking criteria ( the lock hint) in an unsafe manner, only if that succeeds does the... POSIX Threads is a POSIX standard for threads. ...


By adding a mutex to the singleton class, a thread-safe implementation may be obtained. Mutual exclusion (often abbreviated to mutex) algorithms are used in concurrent programming to avoid the concurrent use of un-shareable resources by pieces of computer code called critical sections. ...

 #include <pthread.h> #include <memory> #include <iostream> class Mutex { public: Mutex() { pthread_mutex_init(&m, 0); } void lock() { pthread_mutex_lock(&m); } void unlock() { pthread_mutex_unlock(&m); } private: pthread_mutex_t m; }; class MutexLocker { public: MutexLocker(Mutex& pm): m(pm) { m.lock(); } ~MutexLocker() { m.unlock(); } private: Mutex& m; }; class Singleton { public: static Singleton& Instance(); int example_data; ~Singleton() { } protected: Singleton(): example_data(42) { } private: static std::auto_ptr<Singleton> theSingleInstance; static Mutex m; }; Singleton& Singleton::Instance() { if (theSingleInstance.get() == 0) { MutexLocker obtain_lock(m); if (theSingleInstance.get() == 0) { theSingleInstance.reset(new Singleton); } } return *theSingleInstance; } std::auto_ptr<Singleton> Singleton::theSingleInstance; Mutex Singleton::m; int main() { std::cout << Singleton::Instance().example_data << std::endl; return 0; } 

Note the use of the MutexLocker class in the Singleton::Instance() function. The MutexLocker is being used as an RAII object, also known as scoped lock, guaranteeing that the mutex lock will be relinquished even if an exception is thrown during the execution of Singleton::Instance(), since the language specification pledges that the destructors of automatically allocated objects are invoked during stack unwind. Resource Acquisition Is Initialization, often referred to by the acronym RAII, is a popular design pattern in C++ and D. The technique combines acquisition and release of resources with initialization and uninitialization of objects. ... Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of some condition that changes the normal flow of execution. ...


This implementation invokes the mutex-locking primitives for each call to Singleton::Instance(), even though the mutex is only needed once, the first time the method is called. To get rid of the extra mutex operations, the programmer can explicitly construct Singleton::theSingleInstance early in the program (say, in main); or, the class can be optimized (in this case, using pthread_once) to cache the value of the initialized pointer in thread-local storage. This article is about the computer term. ...


More code must be written if the singleton code is located in a static library, but the program is divided into DLLs.[citation needed] Each DLL that uses the singleton will create a new and distinct instance of the singleton.[citation needed] To avoid that, the singleton code must be linked in a DLL. Alternatively, the singleton can be rewritten to use a memory-mapped file to store theSingleInstance. In computer science, a library is a collection of subprograms used to develop software. ...


C#

This example is thread-safe with lazy initialization. Thread-safety is a computer programming concept applicable to multi-threaded programs. ... In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. ...

 /// <summary> /// Class implements singleton pattern. /// </summary> public class Singleton { // Private constructor to avoid other instantiation // This must be present otherwise the compiler provide  // a default public constructor private Singleton() { } /// <summary> /// Return an instance of <see cref="Singleton"/> /// </summary> public static Singleton Instance { get { /// An instance of Singleton wont be created until the very first  /// call to the sealed class. This is a CLR optimization that ensures that /// we have a properly lazy-loading singleton.  return SingletonCreator.CreatorInstance; } } /// <summary> /// Sealed class to avoid any heritage from this helper class /// </summary> private sealed class SingletonCreator { // Retrieve a single instance of a Singleton private static readonly Singleton _instance = new Singleton(); /// <summary> /// Return an instance of the class <see cref="Singleton"/> /// </summary> public static Singleton CreatorInstance { get { return _instance; } } } } 

Python

According to influential Python programmer Alex Martelli, The Singleton design pattern (DP) has a catchy name, but the wrong focus—on identity rather than on state. The Borg design pattern has all instances share state instead.[3] A rough consensus in the Python community is that sharing state among instances is more elegant, at least in Python, than is caching creation of identical instances on class initialization. Coding shared state is nearly transparent: Python is a high-level programming language first released by Guido van Rossum in 1991. ... Alex Martelli is a member of the Python Software Foundation and works, as of 2006, as Über Tech Lead for Google, Inc. ...

 class Borg: __shared_state = {} def __init__(self): self.__dict__ = self.__shared_state # and whatever else you want in your class -- that's all! 

But with the new style class, this is a better solution, because only one instance is created:

 class Singleton (object): instance = None def __new__(cls, *args, **kargs): if cls.instance is None: cls.instance = object.__new__(cls, *args, **kargs) return cls.instance #Usage mySingleton1 = Singleton() mySingleton2 = Singleton() #mySingleton1 and mySingleton2 are the same instance. assert mySingleton1 is mySingleton2 

Two caveats:

  • The __init__-method is called every time you call Singleton().
  • If you want to inherit from the Singleton-class, instance should probably be a dictionary belonging explicitly to the Singleton-class.
 class InheritableSingleton (object): instances = {} def __new__(cls, *args, **kargs): if InheritableSingleton.instances.get(cls) is None: InheritableSingleton.instances[cls] = object.__new__(cls, *args, **kargs) return InheritableSingleton.instances[cls] 

To create a singleton that inherits from a non-singleton, you must use multiple inheritance.

 class Singleton (NonSingletonClass, object): instance = None def __new__(cls, *args, **kargs): if cls.instance is None: cls.instance = object.__new__(cls, *args, **kargs) return cls.instance 

Be sure to call the NonSingletonClass's __init__ function from the Singleton's __init__ function.


Perl

In Perl 5.10 you can use a state variable.

 package MySingletonClass; use strict; use warnings; use 5.010; sub new { my ($class) = @_; state $the_instance; if (! defined $the_instance) { $the_instance = bless { }, $class; } return $the_instance; } 

In older Perls, just use a closure.

 package MySingletonClass; use strict; use warnings; my $THE_INSTANCE; sub new { my ($class) = @_; if (! defined $THE_INSTANCE) { $THE_INSTANCE = bless { }, $class; } return $THE_INSTANCE; } 

If you want to use Moose, you have the MooseX::Singleton extension module.


Ruby

In Ruby you just include Singleton in your class.

 require 'singleton' class Example include Singleton end 

Prototype-based singleton

In a prototype-based programming language, where objects but not classes are used, a "singleton" simply refers to an object without copies or that is not used as the prototype for any other object. Example in Io: Prototype-based programming is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes. ... Io is a pure object-oriented programming language inspired by Smalltalk, Self, Lisp and NewtonScript. ...

 Foo := Object clone Foo clone := Foo 

Example of use with the factory method pattern

The singleton pattern is often used in conjunction with the factory method pattern to create a system-wide resource whose specific type is not known to the code that uses it. An example of using these two patterns together is the Java Abstract Windowing Toolkit (AWT). The factory method pattern is an object-oriented design pattern. ... The Abstract Windowing Toolkit (AWT) is Javas platform_independent windowing, graphics, and user_interface widget toolkit. ...


java.awt.Toolkit is an abstract class that binds the various AWT components to particular native toolkit implementations. The Toolkit class has a Toolkit.getDefaultToolkit() factory method that returns the platform-specific subclass of Toolkit. The Toolkit object is a singleton because the AWT needs only a single object to perform the binding and the object is relatively expensive to create. The toolkit methods must be implemented in an object and not as static methods of a class because the specific implementation is not known by the platform-independent components. The name of the specific Toolkit subclass used is specified by the "awt.toolkit" environment property accessed through System.getProperties(). In object-oriented programming, a class consists of encapsulated instance variables and subprograms, the methods mentioned below. ... In computer science, binding refers to the creation of a simple reference to something which is larger and more complicated and used frequently. ... A platform-specific model is a model of a software or business system that is linked to a specific technological platform (e. ... In object-oriented programming, a subclass is a class that inherits some properties from its superclass. ... Used mainly in object-oriented programming, the term method refers to a piece of code that is exclusively associated either with a class (called class methods or static methods) or with an object (called instance methods). ...


The binding performed by the toolkit allows, for example, the backing implementation of a java.awt.Window to bound to the platform-specific java.awt.peer.WindowPeer implementation. Neither the Window class nor the application using the window needs to be aware of which platform-specific subclass of the peer is used.


References

  1. ^ Gamma, E, Helm, R, Johnson, R, Vlissides, J: "Design Patterns", page 128. Addison-Wesley, 1995
  2. ^ Alexandrescu, Andrei [2001]. Modern C++ Design. Reading, MA: Addison-Wesley. ISBN 0-201-70431-5. 
  3. ^ Alex Martelli. Singleton? We don't need no stinkin' singleton: the Borg design pattern. ASPN Python Cookbook. Retrieved on 2006-09-07.

Andrei Alexandrescu is widely regarded as one of the foremost experts on advanced C++ programming. ... Modern C++ Design is a book (ISBN 0-201-70431-5) written by Andrei Alexandrescu published in 2001 and subtitled Generic Programming and Design Patterns Applied. The book explores new technique of programming in C++ programming language called template metaprogramming. ... Year 2006 (MMVI) was a common year starting on Sunday of the Gregorian calendar. ... is the 250th day of the year (251st in leap years) in the Gregorian calendar. ...

External links

This article is being considered for deletion in accordance with Wikipedias deletion policy. ... Java bytecode is the form of instructions that the Java virtual machine executes. ... In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. ... This article is about the book by Gamma et al. ... A software design pattern, the Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme. ... The Builder Pattern is a software design pattern. ... The factory method pattern is an object-oriented design pattern. ... A prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. ... In computer programming, the adapter design pattern (often referred to as the wrapper pattern or simply a wrapper) adapts one interface for a class into one that a client expects. ... The bridge pattern is a design pattern used in software engineering which is meant to decouple an abstraction from its implementation so that the two can vary independently (Gamma et. ... This does not cite any references or sources. ... For decorators in Python, see Python syntax and semantics#Decorators. ... The façade pattern is an object-oriented design pattern. ... Flyweight is a software design pattern. ... // In computer programming, the proxy pattern is a software design pattern. ... In Object Oriented Design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. ... In object-oriented programming, the Command pattern is a design pattern in which objects are used to represent actions. ... In computer programming, the interpreter pattern is a particular design pattern. ... In object-oriented programming, an iterator is an object allowing one to sequence through all of the elements or parts contained in some other object, typically a container or list. ... The mediator pattern is a software design pattern that provides a unified interface to a set of interfaces in a subsystem. ... The memento pattern is a software design pattern that provides the ability to restore an object to its previous state (undo by rollback). ... The observer pattern (sometimes known as publish/subscribe) is a design pattern used in computer programming to observe the state of an object in a program. ... A behavioral software design pattern, state pattern is used in computer programming to represent the state of an object. ... In computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected on-the-fly at runtime. ... Template method: UML class diagram. ... In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure. ...

  Results from FactBites:
 
Singleton Pattern (4922 words)
Singletons are most appropriate for services that do not change their nature based on their invocation context.
Singletons are very similar to GlobalVariables, and are often implemented with global variables, even if they masquerade as class members.
Every singleton class is done as an abstract base class and the actual objects constructed in the main line are instances of particular derived classes.
Java Singleton Design Pattern (677 words)
Java Singleton pattern belongs to the family of design patterns, that govern the instantiation process.
The Singleton class’s default constructor is made private, which prevents the direct instantiation of the object by others (Other Classes).
In Summary, the job of the Singleton class is to enforce the existence of a maximum of one object of the same type at any given time.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.