 哈喽!大家好,我是【Bug 终结者】 ,【CSDNJava优质创作者】,阿里云技术博主,51CTO人气博主,INfoQ写作专家 一位上进心十足,拥有极强学习力的【Java领域博主】 【Bug 终结者】博客的领域是【面向后端技术】的学习,未来会持续更新更多的【后端技术】以及【学习心得】。 偶尔会分享些前端基础知识,会更新实战项目,面向企业级开发应用!  如果有对【后端技术】、【前端领域】感兴趣的【小可爱】,欢迎关注【Bug 终结者】 ❤️❤️❤️ 感谢各位大可爱小可爱! ❤️❤️❤️

文章目录

一、什么是单例模式二、哪些地方用到了单例模式三、单例模式的优缺点四、手写单例模式饿汉式襤枚举饿汉式DCL懒汉式双检锁懒汉式洛内部类懒汉式

⛵小结

一、什么是单例模式

单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

注意

单例模式只能由一个实例对象单例类必须自己创建自己的唯一实例。单例类必须给所有其他对象提供这一实例。

二、哪些地方用到了单例模式

单例模式经常用在需要一个实例的程序中,例如

Spring框架IOC容器就使用到了单例模式,默认创建对象的时候为单例模式ResultBean 后端统一返回给前端的封装类,这个在项目中是唯一的,只用一个对象进行返回JSON给前端进行渲染

JDK中也有单例模式的身影,例

Runtime 体现了饿汉式单例Console 体现了双检锁懒汉式单例Collections 中的 EmptyNavigableSet 内部类懒汉式单例ReverseComparator.REVERSE_ORDER 内部类懒汉式单例Comparators.NaturalOrderComparator.INSTANCE 枚举饿汉式单例

三、单例模式的优缺点

优点

提供了对唯一实例的访问可以节约系统资源,提高系统的性能,减少不必要的内存开销允许可变数目的实例(多例类)

缺点

扩展困难(缺少抽象层)单例类的职责过重由于自动垃圾回收机制,可能会导致共享的单例对象的状态丢失

四、手写单例模式

饿汉式

package com.wanshi.single;

//饿汉式单例

public class Hungry {

//会造成资源浪费,占用CPU

private byte[] data1 = new byte[1024*1024];

private byte[] data2 = new byte[1024*1024];

private byte[] data3 = new byte[1024*1024];

private byte[] data4 = new byte[1024*1024];

private Hungry() {

System.out.println("Hungry init...");

}

private static Hungry hungry = new Hungry();

public static Hungry getInstance() {

return hungry;

}

}

class Test {

public static void main(String[] args) {

Hungry hungry = Hungry.getInstance();

Hungry hungry2 = Hungry.getInstance();

System.out.println(hungry);

System.out.println(hungry2);

}

}

襤枚举饿汉式

package com.wanshi.single;

import java.lang.reflect.Constructor;

// enum 是一个class类

public enum EnumSingle {

INSTANCE;

public static EnumSingle getInstance() {

return INSTANCE;

}

}

class Test {

public static void main(String[] args) throws Exception{

EnumSingle instance1 = EnumSingle.INSTANCE;

Constructor declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class, int.class);

declaredConstructor.setAccessible(true);

EnumSingle instance2 = declaredConstructor.newInstance();

System.out.println(instance1);

System.out.println(instance2);

}

}

在这里自行下载jad编译工具即可

枚举类最后反编译源码 jad工具反编译

jad -sjava EnumSingle.class

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.

// Jad home page: http://www.kpdus.com/jad.html

// Decompiler options: packimports(3)

// Source File Name: EnumSingle.java

package com.wanshi.single;

public final class EnumSingle extends Enum

{

public static EnumSingle[] values()

{

return (EnumSingle[])$VALUES.clone();

}

public static EnumSingle valueOf(String name)

{

return (EnumSingle)Enum.valueOf(com/wanshi/single/EnumSingle, name);

}

private EnumSingle(String s, int i)

{

super(s, i);

}

public static EnumSingle getInstance()

{

return INSTANCE;

}

public static final EnumSingle INSTANCE;

private static final EnumSingle $VALUES[];

static

{

INSTANCE = new EnumSingle("INSTANCE", 0);

$VALUES = (new EnumSingle[] {

INSTANCE

});

}

}

DCL懒汉式

package com.wanshi.single;

public class Lazy {

private static Lazy lazy;

public static Lazy getInterface() {

synchronized (Lazy.class) {

if (lazy == null) {

lazy = new Lazy();

}

}

return lazy;

}

public static void main(String[] args) {

Lazy lazy = Lazy.getInterface();

Lazy lazy2 = Lazy.getInterface();

System.out.println(lazy);

System.out.println(lazy2);

}

}

双检锁懒汉式

package com.wanshi.single;

import java.lang.reflect.Constructor;

import java.lang.reflect.Field;

public class LazyMan {

private static boolean flag = false;

private LazyMan() {

synchronized (this) {

if (!flag) {

flag = true;

} else {

throw new RuntimeException("不要试图通过反射破坏对象");

}

}

}

private volatile static LazyMan lazyMan;

//双重检查锁,懒汉式(DCL懒汉式)

public static LazyMan getInstance() {

if (lazyMan == null) {

synchronized (LazyMan.class) {

if (lazyMan == null) {

//不是原子性操作,1.分配内存空间,2.执行构造方法,3.把对象指向这个空间 指令重排可能会发生 加上volatile关闭指令重排

lazyMan = new LazyMan();

}

}

}

return lazyMan;

}

public static void main(String[] args) throws Exception {

// LazyMan lazyMan1 = LazyMan.getInstance();

Field flag = LazyMan.class.getDeclaredField("flag");

flag.setAccessible(true);

Constructor declaredConstructor = LazyMan.class.getDeclaredConstructor(null);

declaredConstructor.setAccessible(true);

LazyMan lazyMan1 = declaredConstructor.newInstance();

flag.set(lazyMan1, false);

LazyMan lazyMan2 = declaredConstructor.newInstance();

System.out.println(lazyMan1);

System.out.println(lazyMan2);

}

}

为什么要使用 volatile 关键字呢 不是原子性操作 1.分配内存空间,2.执行构造方法,3.把对象指向这个空间 指令重排可能会发生 加上volatile关闭指令重排

洛内部类懒汉式

package com.wanshi.single;

public class Holder {

private Holder() {

}

public static class InnerClass {

private static final Holder HOLDER = new Holder();

}

}

案例全部通过测试!

⛵小结

以上就是【Bug 终结者】对手写单例模式及原理剖析的讲解,单例模式共有5种创建方式,分别为饿汉式、DCL懒汉式、双检锁懒汉式、Enum枚举饿汉式,内部类懒汉式,这几种方式要掌握,项目中对于全局唯一的对象将其封装为单例模式,开箱即用,非常方便,以及面试中,会让手写单例模式,可谓是大厂必备!

如果这篇【文章】有帮助到你,希望可以给【Bug 终结者】点个赞,创作不易,如果有对【后端技术】、【前端领域】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 【Bug 终结者】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】!

文章来源

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。