- 什么是单例模式
一个类只有一个全局实例; - 补充说明
一般把其构造方法设为私有,另外提供一个可以获取该实例的静态方法;
由于java存在反射机制,即使是私有构造方法,也能被外部创建,所以一般的写法严格来讲不属于单例模式;(ps:可以在构造方法内加个静态flag标志判断,保证其只能创建一次)
违背了“单一职责原则”,该类既是工厂又是产品(自己创建了自己);
单例模式可以改造成固定大小的多例模式; - 角色
只有一个角色,就是单例; - Java例子
举几个常见的实用的例子
a、在类加载的时候生成对象(如生成该单例对象不耗资源的情况,可以考虑使用)
优点:线程安全;
缺点:不能达到(单例实例在第一次被使用时构建)的效果;
package com.pichen.dp.creationalpattern.singletonpattern.implement1;
public class MyPrinter {
private static MyPrinter myPrinter = new MyPrinter();
private MyPrinter(){
System.out.println("创建了一个MyPrint实例.");
}
public static MyPrinter getInsatnce(){
return myPrinter;
}
public static void testPrint(){
MyPrinter.getInsatnce().print("你好!");
}
public void print(String str){
System.out.println(str);
}
}
2、单例实例在第一次被使用时构建(单线程环境使用)
优点:单例实例在第一次被使用时构建;
缺点:不加锁的话,存在线程安全问题,即使加了锁,对性能也产生了影响;
package com.pichen.dp.creationalpattern.singletonpattern.implement2;
public class MyPrinter {
private static MyPrinter myPrinter = null;
private MyPrinter(){
System.out.println("创建了一个MyPrint实例.");
}
public static synchronized MyPrinter getInsatnce(){
if(null == myPrinter){
myPrinter = new MyPrinter();
}
return myPrinter;
}
public static void testPrint(){
System.out.println("你好!");
}
public void print(String str){
System.out.println(str);
}
}
3、静态内部类(推荐使用)
优点:线程安全;单例实例在第一次被使用时构建;
缺点:暂无发现
package com.pichen.dp.creationalpattern.singletonpattern.implement3;
public class MyPrinter {
private static class MyPrinterHolder {
private static MyPrinter instance = new MyPrinter();
}
private MyPrinter(){
System.out.println("implements3: created a MyPrint instance.");
}
public static MyPrinter getInsatnce(){
return MyPrinterHolder.instance;
}
public static void testPrint(){
System.out.println("hello!");
}
public void print(String str){
System.out.println(str);
}
}
写个测试类如下,当我们没有显示的调用getInsatnce方法的时候,是不会生成单例对象的;
package com.pichen.dp.creationalpattern.singletonpattern.implement3;
import com.pichen.dp.creationalpattern.singletonpattern.implement3.MyPrinter;
public class Main {
public static void main(String[] args) {
// MyPrinter p = MyPrinter.getInsatnce();
// p.print("hello world.");
MyPrinter.testPrint();
}
}
打印结果:
hello!
1.私有化的构造函数
2.私有的静态的全局变量
3.公有的静态的方法
饿汉式:
public class Singleton1 {
private Singleton1() {};
private static Singleton1 single = new Singleton1();
public static Singleton1 getInstance() {
return single;
}
}
懒汉式:
public class Singleton2 {
private Singleton2() {}
private static Singleton2 single=null;
public tatic Singleton2 getInstance() {
if (single == null) {
single = new Singleton2();
}
return single;
}
}
线程安全:(双重判定锁)
public class Singleton3 {
private Singleton3() {}
private static Singleton3 single ;
public static Singleton3 getInstance() {
if(null == single){
synchronized(single ){
if(null == single){
single = new Singleton3();
}
}
}
return single;
}
}
一个线程访问一个对象中的synchronized(this)同步代码块时,其他试图访问该对象的线程将被阻塞。