Singleton design pattern
package swain.singleton.javainterviewhub.blogspot.in.designpattern.singleton;
public class SingleObject {
public static SingleObject singleton = new SingleObject();
private SingleObject() {
}
public static SingleObject getInstance() {
return singleton;
}
public void showMessage(){
System.out.println("hello world");
}
}
package swain.singleton.javainterviewhub.blogspot.in.designpattern.singleton;
public class SingletonPatternDemo {
public static void main(String[] args) {
SingleObject singleObject=SingleObject.getInstance();
singleObject.showMessage();
}
}
There are three cases singleton class fail
1) Serialization :We can create multiple instance by searialization case.
2) Multi threading :Singleton class not thread safe
3) Class loader: We can create multiple instance with different class loader
No comments:
Post a Comment