CSC/ECE 517 Fall 2010/ch3 3f lj: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 19: Line 19:
==Java==
==Java==


Java's API includes a Singleton class which implements the static version of the Singleton pattern.  Java's implementation of the Singleton pattern only allows the programmer to create one instance of the Singleton class.  The Singleton class automatically creates INSTANCE when it is initialized.  The getInstance() method returns the single instance of the class that has been initialized.   
Java's API includes a Singleton class which implements the static version of the Singleton pattern.  Java's implementation of the Singleton pattern only allows the programmer to create one instance of the Singleton class.  The Singleton class automatically creates INSTANCE when it is initialized.  The getInstance() method returns the single instance of the class that has been initialized.  Java also utilizes a constructor to initialize the Singleton class.   


     private static final Singleton INSTANCE = new Singleton();
     private static final Singleton INSTANCE = new Singleton();

Revision as of 05:22, 6 October 2010

Singleton Pattern in Static and Dynamic languages

Singleton pattern

A singleton pattern is a design pattern that restricts the amount of instantiations of a class to one object. This pattern is implemented when exactly one object is needed to coordinate actions across the entire system. The restriction of only having one object can sometimes make the system more efficient and allow the programmer to exercise more control over certain actions in the system. A singleton must satisfy some global access principles, which say all of the classes must be able to access the functionality contained in the singleton class/es.

Implementation

When this pattern is implemented then it must only have one instantiation of a class and that class must satisfy the global access principles. The pattern requires a mechanism to access the singleton class without instantiating another class object and a mechanism to persist the value of class members among class objects.

Creating the instance

The instance can be created with a static constructor in static languages. (See the java example below) This means that the single instance is created on program start up and all new methods are private and never used except by the singleton class itself.

Accessing the instance

In static languages, such as Java, the Singleton class will have a method which returns the instance of the Singleton class so that the other classes can use the functionality encapsulated in the Singleton class.

In dynamic languages, such as Ruby, if a class needs to access the functionality within the Singleton class then it simply needs to access the instance variable @instance and all the functionality therein.

Examples

Java

Java's API includes a Singleton class which implements the static version of the Singleton pattern. Java's implementation of the Singleton pattern only allows the programmer to create one instance of the Singleton class. The Singleton class automatically creates INSTANCE when it is initialized. The getInstance() method returns the single instance of the class that has been initialized. Java also utilizes a constructor to initialize the Singleton class.

   private static final Singleton INSTANCE = new Singleton();
  
   // Private constructor prevents instantiation from other classes
   private Singleton() {
   }
   public static Singleton getInstance() {
       return INSTANCE;
   }

Ruby

Ruby implements the Singleton pattern by privatizing the "new" method and creates an instance variable called @instance. This implementation guarantees that the class will not be able create more than one instantiation of itself and that the system will have the global access privileges since the @instance variable has a global scope.


 include Singleton