在实际开发下,会存在一种情况:某一种类在程序的整个生命周期中,只需要实例化一次就足够了。例如,系统数据类,由于操作系统只有一个,因此在程序初始化时该类只需要实例化一次,之后的系统数据更改都是在这一个实例化对象中进行就可以。

主要是一种控制实例化对象产生个数的设计操作。
class Singleton {
public void print() {
System.out .println("Hello,World")
}
}
public class JavaDemo {
public static void main(String args[]) {
Singleton instanceA = new Singleton();
Singleton instanceB = new Singleton();
Singleton instanceC = new Singleton();
instanceA.print();
instanceB.print();
instanceC.print();
}
}
那么首先就应该禁止单例类在主程序类中的实例化,我们知道,在主程序中的实例化是通过调用构造方法来实现的。而所有的类又默认提供的无参构造,因此首先应在类定义中,私有化构造方法。
private Singleton() {} // 构造方法私有化
这样,我们在若依然主程序类中使用 new 来实例化对象时,就会报错
错误: Singleton() 可以在 Singleton 中访问 private
instance = new Singleton();
private Singleton instance;private static Singleton instance;private static final Singleton INSTANCE = new Singleton();该引用的赋值放在一个方法中,该方法也要定义为类方法:
public static Singleton getInstance() {
return INSTANCE;
}
在系统加载类的时候就会自动提供有 Singleton 类的实例化对象。
方法示例中所实现的就是饿汉式:
class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {} // 构造方法私有化
public static Singleton getInstance() {
return INSTANCE;
}
public void print() {
System.out .println("Hello,World")
}
}
public class JavaDemo {
public static void main(String args[]) {
Singleton instance = null; // 仍可声明对象
instance = Singleton.getInstance();
instance.print();
}
}
在第一次使用的时候才进行实例化对象处理.
这样在定义时就不能使用 final 来限制个数了,而是在获取实例方法中增加判断来限制个数。
class Singleton {
private static Singleton instance;
private Singleton() {} // 构造方法私有化
public static Singleton getInstance() {
if (instance == null){ // 第一次使用
instance = new Singleton() ; // 实例化对象
}
return instance;
}
public void print() {
System.out .println("Hello,World")
}
}
public class JavaDemo {
public static void main(String args[]) {
Singleton instance = null; // 仍可声明对象
instance = Singleton.getInstance();
instance.print();
}
}
多例设计指的是可以保留有多个实例化对象,例如:如果现在要定义一个描述性别的类,那么该对象只有两个:男、 女。或者描述颜色基色的类,可以使用: 红色、绿色、蓝色。这种情况下可以利用多例设计来解决。
class Color { // 定义描述颜色的类
private static final Color RED = new Color("红色");
private static final Color GREEN = new Color("绿色");
private static final Color BLUE = new Color("蓝色");
private String title;
private Color(String title) { // 构造方法私有化
this.title = title;
}
public static Color getInstance(String color){
switch(color){
case "red": return RED;
case "green": return GREEN;
case "blue" : return BLUE;
default : return null;
}
}
public String toString(){
return this.title;
}
}
public class JavaDemo {
public static void main(String args[]) {
Color c = Color.getInstance("green");
System.out.println(c);
}
}
多例设计与单例设计的本质是相同的,一定都会在内部提供有 static 方法以返回实例化对象。