Monday 27 July 2015

The Singleton Pattern

The Singleton Pattern

 

Defined

The Singleton Pattern ensures a class has only one instance, and provides a global point to access it.

Types of objects that we would only need one of : Drivers, registry settings. objects that handle preferences.

Ie.  A Kinect Device Manager object should be a singleton, because a Kinect application should be restricted to only having one open connection to a Kinect Device.


Diagrams




Code Example


CODE DISSECTION
                   Code Solution- to prevent two instances being created on different threads.

Bullet Points

  • The Singleton Pattern ensures you have at most one instance of a class in your application
  • The Singleton Pattern also provides a global access point to that instance. 
  • Java's implementation of the Singleton Pattern makes use of a private constructor, a static method combined with a static variable.
  • Examine your performance and resource constraints and carefully choose an appropriate Singleton implementation for multi-threaded applications (and we should consider all applications multi-threaded)

No comments:

Post a Comment