JAVA设计模式-单例模式

设计模式是什么

设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。

为什么要使用设计模式

提高代码可重用性、让代码更容易被他人理解、保证代码的可读性。也是软件工程的基石,设计模式也是一种思想,使用任何一门面向对象的语言。目前共有23种设计模式。

单例模式解决的问题是什么: 保证类的对象在内存中唯一

步骤:
1.私有化该类的构造函数
2.通过new在本类中创建一个本类对象。
3.定义一个共有的方法将创建的对象返回。

 

单例模式具体两种实现,分别是饿汉模式、懒汉模式。

饿汉模式

/**
 * @Classname Singleton1
 * @Description 单例模式-饿汉模式
 * @Created by xiangty
 * 1. 私有化类的构造方法
 * 2. 创建本类的对象实例
 * 3. 定义一个公共的方法将创建的对象返回
 */
public class Singleton1 {
    // 1. 私有化类的构造方法
    private Singleton1() {
    }

    // 2. 创建本类的对象实例
    private static Singleton1 singleton1 = new Singleton1();

    // 3. 定义一个公共的方法将创建的对象返回
    public static Singleton1 getInstance() {
        return singleton1;
    }

}

懒汉模式

/**
 * @Classname Singleton2
 * @Description 单例模式-懒汉模式
 * @Created by xiangty
 * 1. 私有化类的构造方法
 * 2. 定义一个类的对象
 * 3. 定义一个公共方法将创建的对象返回
 */
public class Singleton2 {

    // 1. 私有化类的构造方法
    private Singleton2() {
    }

    // 2. 定义一个类的对象
    private static Singleton2 singleton2 = null;

    // 3. 定义一个公共方法将创建的对象返回
    public static Singleton2 getInstance() {
        if (singleton2 == null) {
            singleton2 = new Singleton2();
        }
        return singleton2;
    }

}

测试

/**
 * @Classname SingletonTest
 * @Description 单例模式验证
 * @Date 2019/5/11 23:20
 * @Created by xiangty
 */
public class SingletonTest {

    public static void main(String[] args) {
        method1();

        method2();
    }

    /**
     * 验证饿汉模式
     */
    public static void method1(){
        Singleton1 singleton1 = Singleton1.getInstance();
        Singleton1 singleton2 = Singleton1.getInstance();

        if(singleton1 == singleton2){
            System.out.println("饿汉模式:singleton1和singleton2是同一个实例");
        } else {
            System.out.println("饿汉模式:singleton1和singleton2不是同一个实例");
        }
    }

    /**
     * 验证懒汉模式
     */
    public static void method2(){
        Singleton2 singleton1 = Singleton2.getInstance();
        Singleton2 singleton2 = Singleton2.getInstance();

        if(singleton1 == singleton2){
            System.out.println("懒汉模式:singleton1和singleton2是同一个实例");
        } else {
            System.out.println("懒汉模式:singleton1和singleton2不是同一个实例");
        }
    }

}

输出如下:

 

 

/**
 * @Classname SingletonThreadTest
 * @Description 单例模式多线程验证,验证饿汉模式线程安全、懒汉模式线程不安全
 * @Date 2019/5/11 23:20
 * @Created by xiangty
 */
public class SingletonThreadTest implements Runnable{

    public static void main(String[] args) throws Exception {
        SingletonThreadTest singletonThreadTest = new SingletonThreadTest();
        Thread thread1 = new Thread(singletonThreadTest);
        Thread thread2 = new Thread(singletonThreadTest);
        Thread thread3 = new Thread(singletonThreadTest);
        Thread thread4 = new Thread(singletonThreadTest);
        Thread thread5 = new Thread(singletonThreadTest);
        Thread thread6 = new Thread(singletonThreadTest);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
        thread6.start();

        thread1.join();
        thread2.join();
        thread3.join();
        thread4.join();
        thread5.join();
        thread6.join();
    }

    /**
     * 验证饿汉模式
     */
    public static void method1(){
        Singleton1 singleton1 = Singleton1.getInstance();
        Singleton1 singleton2 = Singleton1.getInstance();

        if(singleton1 == singleton2){
            println(Thread.currentThread().getName()+"饿汉模式:singleton1和singleton2是同一个实例");
        } else {
            println(Thread.currentThread().getName()+"饿汉模式:singleton1和singleton2不是同一个实例");
        }
    }

    /**
     * 验证懒汉模式
     */
    public static void method2(){
        Singleton2 singleton1 = Singleton2.getInstance();
        Singleton2 singleton2 = Singleton2.getInstance();

        if(singleton1 == singleton2){
            println(Thread.currentThread().getName()+"懒汉模式:singleton1和singleton2是同一个实例");
        } else {
            println(Thread.currentThread().getName()+"懒汉模式:singleton1和singleton2不是同一个实例");
        }
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            method2();
        }
    }

    /**
     * 输出信息
     */
    private static void println(Object obj){
        System.out.println(obj);
    }

}

输出如下:(测试了很多次才出现的)

 

饿汉模式和懒汉模式区别

    饿汉模式加载类的时候比较慢,获取对象速度快;懒汉加载类时过快,获取对象速度过慢。

    饿汉模式线程安全、懒汉模式线程不安全

 

懒汉式在多线程中的安全隐患以及解决方案、优化策略。

上图在验证懒汉模式的时候,出现了创建多个实例的安全隐患。

原因是懒汉式多加了一次判断(如下图),导致了线程安全性隐患。因为CPU很有可能在执行完if语句之后切向其它线程。解决线程安全性问题的关键就是加上同步锁。

方法1:使用同步函数,使用synchronized关键字修饰方法

Singleton2.java类中添加代码如下:

/**
 * (优化方法1:使用同步函数) 定义一个公共方法将创建的对象返回
 */
public static synchronized Singleton2 getInstance() {
	if (singleton2 == null) {
		singleton2 = new Singleton2();
	}
	return singleton2;
}

方法1的不足之处,直接使用synchronized关键字修饰方法效率比较低,因为每次调用getInstance方法需要先判断锁。

方法2:使用同步代码块

/**
 * (优化方法2:使用同步代码块)
 */
public static Singleton2 getInstanceNew2(){
	synchronized(Singleton2.class){
		if(singleton2 == null){
			singleton2 = new Singleton2();
		}
		return singleton2;
	}
}

不足之处,与方法1相比效率基本没有变化

方法3:使用同步代码块解决效率问题

/**
 * 优化方法3
 */
public static Singleton2 getInstance(){
	if(singleton2 == null) { // 解决效率问题
		synchronized (Singleton2.class) {
			if (singleton2 == null) { // 第二次判空操作是为了保证单例对象的唯一性
				singleton2 = new Singleton2();
			}
		}
	}
	return singleton2;
}

与方法2相比,代码块外多了一层判断,主要是为了解决被多次执行代码块的效率问题。

方法3实现步骤:

        如果没有创建对象,即singleton2 ==null,通过第一层判断;再进入同步代码块,进入同步代码块则其它线程则要创建实例就需要获取锁;等当前线程创建好对象后,释放锁以后,假如有其它线程已经进入代码块,那么会通过第二层if判断来判断对象实例的唯一性。没有进入第一层if判断的时候,判空操作之后不满足条件,直接放回对象。后续调用getInstance方法,只会执行第一层if判断,不会再执行同步代码块了,大大提高了执行效率。

 

如有问题,请指正,谢谢!

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值