请问单态模式什么时候用,我以前看JAVA基础也没有看到讲过他,请大家发言,谈谈自己的看法,代码如下:
//单态模式测试类
public class SingletonTest {
//该类的一个普通属性
int value;
//使用静态属性类保存该类的一个属性
private static SingletonTest instance;
//构造器私有化,避免该类被多次实例
private SingletonTest(){
System.out.println("正在执行构造器....");
}
//提供静态方法来返回该类的实例
public static SingletonTest getInstance(){
//实例化类实例前,先检查该类的的实例是否存在
if(instance==null){
//如果不存在,则新建一个实例
instance = new SingletonTest();
}
//返回该类的成员变量:该类的实例
return instance;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SingletonTest t1 = SingletonTest.getInstance();
SingletonTest t2 = SingletonTest.getInstance();
t1.setValue(20);
t2.setValue(0);
System.out.println(t1);
System.out.println(t2); }
}
//单态模式测试类
public class SingletonTest {
//该类的一个普通属性
int value;
//使用静态属性类保存该类的一个属性
private static SingletonTest instance;
//构造器私有化,避免该类被多次实例
private SingletonTest(){
System.out.println("正在执行构造器....");
}
//提供静态方法来返回该类的实例
public static SingletonTest getInstance(){
//实例化类实例前,先检查该类的的实例是否存在
if(instance==null){
//如果不存在,则新建一个实例
instance = new SingletonTest();
}
//返回该类的成员变量:该类的实例
return instance;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SingletonTest t1 = SingletonTest.getInstance();
SingletonTest t2 = SingletonTest.getInstance();
t1.setValue(20);
t2.setValue(0);
System.out.println(t1);
System.out.println(t2); }
}
可惜,我不会Java的泛型
第一种:public class SingletonTest {
int value;
private static SingletonTest instance; private SingletonTest() {
System.out.println("正在执行构造器....");
} public synchronized static SingletonTest getInstance() { //注意这行,必须要同步一下
if (instance == null)
instance = new SingletonTest();
return instance; } public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} public static void main(String[] args) {
SingletonTest t1 = SingletonTest.getInstance();
SingletonTest t2 = SingletonTest.getInstance();
t1.setValue(20);
t2.setValue(0);
System.out.println(t1);
System.out.println(t2);
}
}
第二种:public class SingletonTest {
int value;
private static SingletonTest instance = new SingletonTest(); private SingletonTest() {
System.out.println("正在执行构造器....");
} public synchronized static SingletonTest getInstance() { //注意这行
return instance; } public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} public static void main(String[] args) {
SingletonTest t1 = SingletonTest.getInstance();
SingletonTest t2 = SingletonTest.getInstance();
t1.setValue(20);
t2.setValue(0);
System.out.println(t1);
System.out.println(t2);
}
}
int value;
private static SingletonTest instance = new SingletonTest(); //注意这行 private SingletonTest() {
System.out.println("正在执行构造器....");
} public static SingletonTest getInstance() {
return instance; } public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} public static void main(String[] args) {
SingletonTest t1 = SingletonTest.getInstance();
SingletonTest t2 = SingletonTest.getInstance();
t1.setValue(20);
t2.setValue(0);
System.out.println(t1);
System.out.println(t2);
}
}
这是WPS开发人员最基本的要求。