java_单例设计模式

什么是设计模式

什么是单例设计模式

单例设计模式------饿汉式

虽然你没有使用这个对象实例,但是它也帮你创建了!容易造成对象的浪费

對象,通常是重量級的對象, 餓漢式可能造成創建了對象,但是沒有使用.

java 复制代码
package com.hspedu.single_;

public class SingleTon01 {
    public static void main(String[] args) {
        // GirlFriend xh = new GirlFriend("小红");
        // GirlFriend xb = new GirlFriend("小白");
        //通过方法可以获取对象
        GirlFriend instance = GirlFriend.getInstance();
        System.out.println(instance);
        GirlFriend instance2 = GirlFriend.getInstance();
        System.out.println(instance2);
        System.out.println(instance == instance2);//T
        //System.out.println(GirlFriend.n1);

    }
}

//有一个类, GirlFriend
//只能有一个女朋友
class GirlFriend {
    private String name;

    //public static int n1 = 100;
    //为了能够在静态方法中,返回 gf 对象,需要将其修饰为 static
    //對象,通常是重量級的對象, 餓漢式可能造成創建了對象,但是沒有使用.
    private static GirlFriend gf = new GirlFriend("小红红");

    //如何保障我们只能创建一个 GirlFriend 对象
    //步骤[单例模式-饿汉式]
    //1. 将构造器私有化
    //2. 在类的内部直接创建对象(该对象是 static)
    //3. 提供一个公共的 static 方法,返回 gf 对象
    private GirlFriend(String name) {
        System.out.println("構造器被調用.");
        this.name = name;
    }

    public static GirlFriend getInstance() {
        return gf;
    }

    @Override
    public String toString() {
        return "GirlFriend{" +
                "name='" + name + '\'' +
                '}';
    }
}

单例设计模式------懒汉式

java 复制代码
package com.hspedu.single_;

public class SingleTon02 {

    public static void main(String[] args) {
//new Cat("大黃");
//System.out.println(Cat.n1);
        Cat instance = Cat.getInstance();
        System.out.println(instance);
//再次調用 getInstance
        Cat instance2 = Cat.getInstance();
        System.out.println(instance2);
        System.out.println(instance == instance2);//T
    }
}
//希望在程序運行過程中,只能創建一個 Cat 對象
//使用單例模式
class Cat {
    private String name;
    public static int n1 = 999;
    private static Cat cat ; //默认是 null
    //步驟
//1.仍然构造器私有化
//2.定義一個 static 静态属性对象
//3.提供一個 public 的 static 方法,可以返回一個 Cat 對象
//4.懒汉式,只有當用戶使用 getInstance 時,才返回 cat 對象, 後面再次調用時,會返回上次創建的 cat 對象
// 从而保证了单例
    private Cat(String name) {
        System.out.println("構造器調用...");
        this.name = name;
    }
    public static Cat getInstance() {
        if(cat == null) {//如果還沒有創建 cat 對象
            cat = new Cat("小可愛");
        }
        return cat;
    }
    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                '}';
    }
}

饿汉式VS懒汉式

小结:

1)单例模式的两种实现方式(1)饿汉式(2)懒汉式

2)饿汉式的问题:在类加载时候就创建,可能存在资源浪费问题

3)懒汉式的问题:线程安全问题,后面我们学了线程后,在进行完善

4)要求大家可以自己独立写出单例模式

相关推荐
_哆啦A梦6 小时前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
考虑考虑7 小时前
JDK25模块导入声明
java·后端·java ee
_小马快跑_8 小时前
Java 的 8 大基本数据类型:为何是不可或缺的设计?
java
Re_zero10 小时前
线上日志被清空?这段仅10行的 IO 代码里竟然藏着3个毒瘤
java·后端
洋洋技术笔记11 小时前
Spring Boot条件注解详解
java·spring boot
程序员清风1 天前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
皮皮林5511 天前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
华仔啊1 天前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing1 天前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠2 天前
各版本JDK对比:JDK 25 特性详解
java