Visual Studio Code搭建VUE开发环境

乐百川

关注

阅读 29

2024-08-11

单例设计模式

懒汉式:
/*
懒汉式
 */
public class Student {
    //创建static修饰的成员变量
    private static Student student;

    //设计私有构造方法
    public Student() {
        super();
    }

    //提供共有的方法
    public static synchronized Student getInstance(){
        if(student==null) {
            student= new Student();
        }
        return student;
    }
}
饿汉式:
/*
饿汉式
 */
public class Student {
    //创建static修饰的成员变量
    private static Student stu=new Student();
    //设计私有构造方法
    public Student() {
        super();
    }
    //提供共有的方法
    public static synchronized Student getInstance(){
        return stu;
    }
}

精彩评论(0)

0 0 举报