Tuesday, 11 February 2014

How To...

Singleton Pattern:
"Ensures a class has only one instance, and provides a global point of access to it."
Here we are making a class , which has only one instance which is instanciated within itself and we never let any other class to make another instance.
we are providing a global access point to the instance. - Head first design pattern.
ex: servelets.
This picture is taken from  http://www.oodesign.com/singleton-pattern.html.

When restricting access , we always have to think about make something a "private". The method which instanciates object is the constructor. So we make constructor "private". When constuctor made private, it should be instanciated in a static method/variable so as to give global access. When you think of global access , make it thread safe.
public class Singleton{
private static Singleton uniqueInstance;
private Singleton(){}
public static synchronized Singleton getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton();
}
return uniqueInstance;
}
}
To improve Multitreading
1) Do nothing if the performance of getInstance() is not critical
2)If your application always creates and uses singleton then create Singleton eagerly as JVM creates it when the class is loaded.
       public class Singleton {
          private static Singleton uniqueInstance = new Singleton();
      private Singleton(){}
    public static Singleton getInstance() {
        return uniqueInstance;
    JVM ensures that instance will be created before any thread can access to the static instance variable.
3)Use double-checked locking to reduce the use of sychronization in getInstance() - synchronization kills performance
public class Singleton{
   private volatile static Singleton uniqueInstance;
private Singleton(){}
public static Singleton getInstance(){
if (uniqueInstance == null){  /** only enter the block if it is null. this means it enters synchronized block only once for the first time.**/
synchronized (Singleton.class){
if (uniqueInstance == null)   /** double checking just in case first one checked by  two threads simultaniously  as one of them will succeeds,  other one waits just outside after first checking.**/
   uniqueInstance = new Singleton();
}
}
return uniqueInstance;
}
}
These are all referenced from "Head First Design Patterns" Since I do not want to mess  up with singleton pattern.
Time for our own Singletons. Let us think of Central heating System , and house as a class loader.(one singleton per one class loader). It has only one instance and anybody in the house can have access to it (global) .
I create them eagerly since I bought my house in the winter.(just kidding)

public class HeatingSystem {
private static HeatingSystem uniqueInstance = new HeatingSystem();
private static Temperature ;
//currentTime == calender.getTime();
private HeatingSystem(){}
public static HeatingSystem getInstance(){
return uniqueInstance;
}
public void setTimer(Time startTime, Time endTime){
        if (currentTime.equalsTo( startTime))
            onSystem();
       else if (currentTime .equalsTo(endTime))
            offSystem();
/** to get current time you can use
Calendar calender = Calendar.getInstance();
CurrentTime = calender.getTime();**/

}
public  void setTemp(Temperature temp)
{
        this.Temperature = temp;
}
public void onSytem(){
    // press the button
}
public void offSystem(){
//undo the pressed button
}
}
Now everybody has only one instance , and has global access to that instance. Husband gets the instance switch on the system, He forgets all about it and when he sees the gas bill he could  blame it on the wife since she had the same instance  and access, so he expected her to switched it off as it is her responsibility too.

How to fight over your Gas Bill. (All  characters here are fictitious , if it resembles someone or some incidents then it is purely coincidental).


No comments:

Post a Comment