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 4: Line 4:


== <b>Implementation</b> ==
== <b>Implementation</b> ==
The Singleton pattern is implemented by <br>
- The constructor of this class is turned to private.<br>
- Create a class with a method which will create a new instance of the class (If one doesn't already exist)<br>
- The constructor of this class is turned to private or protected<br>
- Another public function is used to call this constructor.<br>  
- Another public function is used to call this constructor.<br>  
- If the object is not initialized yet (object == null) it calls the constructor and returns a reference else it will just return a reference.<br>
- If the object is not initialized yet (object == null) it calls the constructor and returns a reference else it will just return a reference.<br>


In multithreading environment, a synchronized command should be used so every thread in the program cannot initialize its own instance of the singleton class.
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.


== <b>Example Implementations</b> ==


<b>Java</b>
<b>Java</b>
Line 19: Line 15:
public class LoggerSingleton {
public class LoggerSingleton {
   private static LoggerSingleton instance = null;
   private static LoggerSingleton instance = null;
   protected LoggerSingleton() {
   private LoggerSingleton() {
       // Exists only to defeat instantiation.
       // Exists only to defeat instantiation.
   }
   }
Line 28: Line 24:
       return instance;
       return instance;
   }
   }
  // add other attributes
}
}


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


import org.apache.log4j.Logger;
 
import junit.framework.Assert;
import junit.framework.Assert;  
import junit.framework.TestCase;
import junit.framework.TestCase;  
public class SingletonTest extends TestCase {
 
  private LoggerSingleton sone = null, stwo = null;
public class TestSingleton extends TestCase {
  private static Logger logger = Logger.getRootLogger();
  private LoggerSingleton logger1 = null, logger2 = null;
  public SingletonTest(String name) {
  public TestSingleton(String name) {
      super(name);
    super(name);
  }
  }
  public void setUp() {
  public void setUp() {
      logger.info("getting singleton...");
logger1 = LoggerSingleton.getInstance();
      sone = LoggerSingleton.getInstance();
        logger2 = LoggerSingleton.getInstance();
      logger.info("...got singleton: " + sone);
  }
      logger.info("getting singleton...");
  public void testUnique() {
      stwo = LoggerSingleton.getInstance();
System.out.println(logger1);
      logger.info("...got singleton: " + stwo);
System.out.println(logger2);
  }
Assert.assertEquals(true, logger1 == logger2);
  public void testUnique() {
  }
      logger.info("checking singletons for equality");
      Assert.assertEquals(true, sone == stwo);
  }
}
}


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

Revision as of 16:25, 1 October 2007

Singleton Pattern

The Singleton pattern is a design pattern used to insure the existence of a single instance of a class in the run time. It constructs the object once and returns a reference to this object every time thereafter. 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;
  private LoggerSingleton() {
     // Exists only to defeat instantiation.
  }
  public static LoggerSingleton getInstance() {
     if(instance == null) {
        instance = new LoggerSingleton();
     }
     return instance;
  }
  // add other attributes

}


Ruby

require 'singleton'

class LoggerSingleton

 include Singleton 

end


Test singletons

Following a code to test singleton class in Java:


import junit.framework.Assert; import junit.framework.TestCase;

public class TestSingleton extends TestCase {

 private LoggerSingleton logger1 = null, logger2 = null;
 public TestSingleton(String name) {
   super(name);
 }
 public void setUp() {

logger1 = LoggerSingleton.getInstance();

       logger2 = LoggerSingleton.getInstance();
 }
 public void testUnique() {

System.out.println(logger1); System.out.println(logger2); Assert.assertEquals(true, logger1 == logger2);

 }

}

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.