发布于 2016-07-25 04:43:24 | 106 次阅读 | 评论: 0 | 来源: 网友投递
Java程序设计语言
java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。
package singletonpattern;
public class Singleton1 {
private static Singleton1 uniqueInstance;
private Singleton1() {
}
public static Singleton1 getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton1();
}
return uniqueInstance;
}
}
package singletonpattern;
public class Singleton2 {
private static Singleton2 uniqueInstance;
private Singleton2() {
}
public static synchronized Singleton2 getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Singleton2();
}
return uniqueInstance;
}
}
package singletonpattern;
public class Singleton3 {
private static Singleton3 uniqueInstance = new Singleton3();
private Singleton3() {
}
public static Singleton3 getInstance() {
return uniqueInstance;
}
}
public class Singleton5 {
private Singleton5(){
}
public static Singleton5 getInstance(){
return Nested.instance;
}
static class Nested{
static Singleton5 instance = new Singleton5();
}
}
package singletonpattern;
public class Singleton4 {
private volatile static Singleton4 uniqueInstance;
private Singleton4() {
}
public static Singleton4 getInstance() {
if (uniqueInstance == null) {
synchronized (Singleton4.class) {
if (uniqueInstance == null) {
uniqueInstance = new Singleton4();
}
}
}
return uniqueInstance;
}
}