-
Efficient Singleton example (4 messages)
- Posted by: stanimir 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 (4)
- 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
-
example of singleton pattern[ Go to top ]
- Posted by: lkssal lkssal
- Posted on: February 14 2005 09:57 EST
- in response to stanimir 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
/***
*/
public TestSingleton() {
// TODO Auto-generated constructor stub
oSingletonClass.setI(10);}
public void TestSingletonX() {
// TODO Auto-generated constructor stubSingletonClass oSingletonClassX = SingletonClass.getInstance();
System.
out.println(oSingletonClassX.getI());}
/***
@param
args
*/
public static void main(String[] args) {
// TODO Auto-generated method stubTestSingleton 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();
}