2018年08月30日
目录
1、单例模式概念
- 确保某一个类只有一个实例,并且自行实例化,并且向整个系统提供这个实例。
- 与全局变量的区别:全局变量不能实现继承,而单例模式可以;
2、饿汉模式
package designModel.SingleExampleModel;
public class testSingleExample {
public testSingleExample() {}
private static testSingleExample test = new testSingleExample();
public static testSingleExample getInstance(){
if(test == null){
System.out.println("未实例化。。");
}
return test;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("1 "+testSingleExample.getInstance());
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i =1; i<11; i++){
System.out.println(i+" "+testSingleExample.getInstance());
}
}
});
thread.start();
}
}
说明:饿汉模式,即对象实例化发生在编译时,static 静态变量被初始化了。
console:
1 designModel.SingleExampleModel.testSingleExample@5ce345c2
1 designModel.SingleExampleModel.testSingleExample@5ce345c2
2 designModel.SingleExampleModel.testSingleExample@5ce345c2
3 designModel.SingleExampleModel.testSingleExample@5ce345c2
4 designModel.SingleExampleModel.testSingleExample@5ce345c2
5 designModel.SingleExampleModel.testSingleExample@5ce345c2
6 designModel.SingleExampleModel.testSingleExample@5ce345c2
7 designModel.SingleExampleModel.testSingleExample@5ce345c2
8 designModel.SingleExampleModel.testSingleExample@5ce345c2
9 designModel.SingleExampleModel.testSingleExample@5ce345c2
10 designModel.SingleExampleModel.testSingleExample@5ce345c2
可见上面获取到的对象都是同一个,单例模式效果。
3、懒汉模式
package designModel.SingleExampleModel;
public class testSingleExampleOfLazyType {
public testSingleExampleOfLazyType() {}
public static testSingleExampleOfLazyType test = null;
public static testSingleExampleOfLazyType getInstance(){
if(test == null){
System.out.println("懒汉模式==实例化。。。");
test = new testSingleExampleOfLazyType();
return test;
}else {
return test;
}
}
public static void main(String [] args){
System.out.println(testSingleExampleOfLazyType.getInstance());
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 1;i<11;i++){
System.out.println(i+" "+testSingleExampleOfLazyType.getInstance());
}
}
});
thread.start();
}
}
说明:懒汉模式即,对象的实例化(内存分配)发生在被调用的那一刻。
懒汉模式==实例化。。。
designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
1 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
2 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
3 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
4 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
5 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
6 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
7 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
8 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
9 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
10 designModel.SingleExampleModel.testSingleExampleOfLazyType@5ce345c2
只有当单例模式提供的外部接口被调用,对象才发生实例化。