1.单例模式的定义
确保一个类只有一个实例,而且自行实例化向整个系统提供这个实例。
//饿汉式
public class Singleton {
private static final Singleton singleton = new Singleton();
//限制产生多个对象
private Singleton() {
}
//通过该方法获取实例对象
public static Singleton getEmperor() {
return singleton;
}
//其他方法,尽量是static
public static void doSomething() {
}
}
2.单例模式的应用
优点:
-减少了内存开支
-减少了系统的性能开销
-避免对资源的多重占用
-设置全局访问点,优化和共享资源访问
缺点:
-扩展困难
-对测试不利
-与单一职责原则有冲突
应用场景:
-要求生成唯一序列号
-在整个项目中需要一个共享访问点或共享数据,如web页面上的计数器
-创建一个对象需要消耗的资源过多,如要访问IO和数据库等
-需要定义大量的静态常量和静态方法(工具类)的环境,(也可以直接声明为static)
3.线程安全问题
单例线程不安全的方式
//懒汉式
public class Singleton {
private static Singleton singleton = null;
//限制产生多个对象
private Singleton() {
}
//通过该方法获取实例对象
public static Singleton getEmperor() {
if(singleton == null){
singleton = new Singleton();
}
return singleton;
}
//其他方法,尽量是static
public static void doSomething() {
}
}
解决线程安全的方法,可以在方法前加synchronized ,也可以在方法内增加synchronized 。
扩展
有上限的单例模式
public class Singleton {
//定义最多能产生的实例数量
private static int maxNUmofSingleton = 2;
//保存每个实例的名字
private static ArrayList<String> nameList = new ArrayList<>();
//放置所有的实例
private static ArrayList<Singleton> singletonList = new ArrayList<>();
//实例的序列号
private static int countNumOfSinglton = 0;
//产生所有的对象
static{
for (int i = 0; i<maxNUmofSingleton;i++){
singletonList.add(new Singleton("第"+i+1+"个"));
}
}
//限制产生多个对象
private Singleton(String name) {
nameList.add(name);
}
//通过该方法获取实例对象
public static synchronized Singleton getSingleton() {
Random random = new Random();
countNumOfSinglton = random.nextInt(maxNUmofSingleton);
return singletonList.get(countNumOfSinglton);
}
//其他方法,尽量是static
public static void doSomething() {
System.out.println(nameList.get(countNumOfSinglton));
}
测试方法如下
public static void main(String[] args) {
int mini = 5;
for (int i = 0; i < mini; i++) {
Singleton singleton = Singleton.getSingleton();
System.out.println("第"+(i+1)+"个实例是:");
singleton.doSomething();
}
}