Tuesday, October 22, 2013

Singleton in Android

Singleton is a part of Gang of Four design pattern and it is categorized under creational design patterns.

-> Static member : This contains the instance of the singleton class.
-> Private constructor : This will prevent anybody else to instantiate the Singleton class.
-> Static public method : This provides the global point of access to the Singleton object and returns the instance to the client calling class.

1.create private instance
2.create private constructor
3.use getInstance() of Singleton class


public class Logger{
private static Logger objLogger;
private Logger(){
//ToDo here
}

public static Logger getInstance() {
if (objLogger == null) {
objLogger = new Logger();
}
return objLogger;
}
}