010Java基础之单例设计模式

1、什么事设计模式

设计模式是在大量的实践中总结和理论化之后优选的代码结构、编程风格、以及解决问题的思考方式。设计模式就像是金典的棋谱,不同的棋局,我们用不同的棋谱,免去我们自己再去思考和摸索。

2、单例设计模式

所谓单例设计模式,就是采用一定的方法保证在整个软甲系统中,对某个类只能存在一个对象实例,并且该类只提供一个获取其对象实例的方法。实现步骤:

1、构造器私有化

防止用户直接去new

2、类的内部创建对象
3、向外暴露一个静态的公共方法
1、饿汉式

饿汉式在类加载时就创建了对象

/**
 * @author Francis
 * @create 2021-07-01 22:21
 */
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 + '\'' +
                '}';
    }

}

在这里插入图片描述

2、懒汉式

懒汉式在使用时再创建对象

/**
 * @author Francis
 * @create 2021-07-01 22:41
 */
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)创建时机不同
饿汉式是在类加载的时候就创建了对象实例,而懒汉式是在使用时才创建对象实例
(2)饿汉式不存在线程安全问题,懒汉式存在线程安全问题
在这里插入图片描述
(3)饿汉式存在资源浪费问题,因为如果程序员一个对象实例都没有使用,name饿汉式创建的对象就浪费了,懒汉式是使用时才创建,不存在资源浪费问题
(4)单例模式在javaSE中的金典例子:java.lang.Runtime

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值