单例设计模式
懒汉式:
/*
懒汉式
*/
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;
}
}