-
Efficient Singleton example (5 messages)
- Posted by: Stan Iakov
- Posted on: February 06 2005 11:38 EST
Hi guys,
I need to build a Cache Object on a Web server.
So I want to use the Singleton design pattern.
So I need some resources, documentation and exemples on the Singleton pattern.
Any links to resources will be welcome.
Thank you.
StanThreaded Messages (5)
- example of singleton pattern by lkssal lkssal on February 14 2005 09:57 EST
- example of singleton pattern by Colin Yates on February 14 2005 10:36 EST
-
example of singleton pattern by Sonjoy chakraborty on January 10 2011 04:11 EST
- example of singleton pattern by Sonjoy chakraborty on January 10 2011 04:15 EST
-
example of singleton pattern by Sonjoy chakraborty on January 10 2011 04:11 EST
- example of singleton pattern by Colin Yates on February 14 2005 10:36 EST
- Singleton Design Pattern by Anil Nivargi on June 28 2014 14:28 EDT
-
example of singleton pattern[ Go to top ]
- Posted by: lkssal lkssal
- Posted on: February 14 2005 09:57 EST
- in response to Stan Iakov
this is an exemple of implementation of Singleton PATTERN
if you want to access to your singleton
MyClassSingleton singleton = MyClassSingleton.getInstance();
singleton.method1();
/*************/
public class MyClassSingleton {
private static MyClassSingleton instance;
//Constructor must be protected or private to perevent creating new object
protected MyClassSingleton() {
}
//could be synchronized
public static MyClassSingletongetInstance() {
if (instance==null)
instance = new MyClassSingleton()
return instance;
}
public void method1(){
System.out.println("hello singleton");
}
}//end of class -
example of singleton pattern[ Go to top ]
- Posted by: Colin Yates
- Posted on: February 14 2005 10:36 EST
- in response to lkssal lkssal
To avoid thread safety issues:
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {
}
public static Singleton getInstance() { return INSTANCE; }
} -
example of singleton pattern[ Go to top ]
- Posted by: Sonjoy chakraborty
- Posted on: January 10 2011 16:11 EST
- in response to Colin Yates
/**
* @author Sonjoy Chakraborty
*
*/
public class SingletonClass {private static final SingletonClass oSingletonClass = new SingletonClass();
public int i = 0;public int getI() {
return i;
}public void setI(int i) {
this.i = i;
}private SingletonClass() {
// TODO Auto-generated constructor stub
}public static synchronized SingletonClass getInstance() {
return oSingletonClass;
}}
// Now real implementation [ to confirm that the same instance is in all the cases. ]
/**
*
@author
Sonjoy
Chakraborty
*
*/
public
*
*/
}
SingletonClass oSingletonClassX = SingletonClass.getInstance();
System.
out.println(oSingletonClassX.getI());}
*
@param
args
*/
TestSingleton oTestSingleton =
new TestSingleton();oTestSingleton.TestSingletonX();
}
}
class TestSingleton {SingletonClass oSingletonClass = SingletonClass.getInstance();
}
}
-
example of singleton pattern[ Go to top ]
- Posted by: Sonjoy chakraborty
- Posted on: January 10 2011 16:15 EST
- in response to Sonjoy chakraborty
public static void main(String[] args) {
// TODO Auto-generated method stub
TestSingleton oTestSingleton = new TestSingleton();
oTestSingleton.TestSingletonX();
}
-
Singleton Design Pattern[ Go to top ]
- Posted by: Anil Nivargi
- Posted on: June 28 2014 14:28 EDT
- in response to Stan Iakov
There are many approaches to implement the Sigleton Design Pattern like Eager Initialization,Lazy Initialization,static block initialization,Thread safe singleton
If anybody wants to know all these approaches in detail then please go through this blog http://adnjavainterview.blogspot.in/2014/06/singleton-design-pattern-in-java-with.html