CSC/ECE 517 Fall 2007/wiki1b 6 c1: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
Illustration
<b>Illustration</b>
The singleton pattern is a pattern used to insure the existence of a single instance of the class in the run time. As it construct the object once and return a reference to this object every time later. In a multithreading environment, special care should be taken, as it will be illustrated later in this page.
<br>The singleton pattern is a pattern used to insure the existence of a single instance of the class in the run time. As it construct the object once and return a reference to this object every time later. In a multithreading environment, special care should be taken, as it will be illustrated later in this page.


Implementation
<b>Implementation</br><br>
- The constructor of this class is turned to private.
- The constructor of this class is turned to private.
- Another public function is used to call this constructor.  
- Another public function is used to call this constructor.  
Line 10: Line 10:




Java
<b>Java</b>


public class LoggerSingleton {
public class LoggerSingleton {
Line 27: Line 27:




Ruby  
<b>Ruby</b>


require 'singleton'  
require 'singleton'  
Line 37: Line 37:




Test singletons
<b>Test singletons


Following a code to test singleton class in Java:
Following a code to test singleton class in Java:</b>


import org.apache.log4j.Logger;
import org.apache.log4j.Logger;
Line 65: Line 65:




in ruby;
<b>in ruby;</b>


def test_singleton
def test_singleton
Line 80: Line 80:




Conclusion
<b>Conclusion</b>


Java doesn’t contain singleton pattern in the language itself, although implementation for the pattern is still possible. While in Ruby the singleton pattern is in the language itself, which became possible since Ruby is a dynamic language through singleton module.
Java doesn’t contain singleton pattern in the language itself, although implementation for the pattern is still possible. While in Ruby the singleton pattern is in the language itself, which became possible since Ruby is a dynamic language through singleton module.

Revision as of 14:15, 1 October 2007

Illustration
The singleton pattern is a pattern used to insure the existence of a single instance of the class in the run time. As it construct the object once and return a reference to this object every time later. In a multithreading environment, special care should be taken, as it will be illustrated later in this page.

Implementation

- The constructor of this class is turned to private. - Another public function is used to call this constructor. - If the object is not initialized yet (object == null) it calls the constructor and returns a reference else it will just return a reference.

In multithreading environment, a synchronized command should be used so not every thread in the program can initialize its own instance of the singleton class.


Java

public class LoggerSingleton {

  private static LoggerSingleton instance = null;
  protected LoggerSingleton() {
     // Exists only to defeat instantiation.
  }
  public static LoggerSingleton getInstance() {
     if(instance == null) {
        instance = new LoggerSingleton();
     }
     return instance;
  }

}


Ruby

require 'singleton'

class LoggerSingleton

 include Singleton 

end


Test singletons

Following a code to test singleton class in Java:

import org.apache.log4j.Logger; import junit.framework.Assert; import junit.framework.TestCase; public class SingletonTest extends TestCase {

  private LoggerSingleton sone = null, stwo = null;
  private static Logger logger = Logger.getRootLogger();
  public SingletonTest(String name) {
     super(name);
  }
  public void setUp() {
     logger.info("getting singleton...");
     sone = LoggerSingleton.getInstance();
     logger.info("...got singleton: " + sone);
     logger.info("getting singleton...");
     stwo = LoggerSingleton.getInstance();
     logger.info("...got singleton: " + stwo);
  }
  public void testUnique() {
     logger.info("checking singletons for equality");
     Assert.assertEquals(true, sone == stwo);
  }

}


in ruby;

def test_singleton

 class << OnlyOne.instance
   def reset
     # reset state
   end
 end
 OnlyOne.instance.modify_state
 OnlyOne.instance.reset

end


Conclusion

Java doesn’t contain singleton pattern in the language itself, although implementation for the pattern is still possible. While in Ruby the singleton pattern is in the language itself, which became possible since Ruby is a dynamic language through singleton module.