比较常见的单例设计模式: 单例设计模式又分为两种:
第一种饿汉式:
class Students
{
private Students()
{
}
private static Students st = new Students();
public static Students getStudents()
{
return st;
}
}
第二种 懒汉式(延迟加载设计模式)
class Single //延迟加载单例模式
{
private static Single s = null;
private Single(){}
public static Single getInst()
{
if(s == null){
synchronized(Single.class)
{
if (s == null)
{
s = new Single();
}
}
}
return s;
}
}
|