Java单例模式
程序员文章站
2022-07-13 23:46:54
...
下面是三种设计模式的实现。
package cn.cnic.lifx.singleton;
public class Singleton
{
private Singleton(){ }
private static class SingletonFactory
{
static Singleton instance = new Singleton();
}
public static Singleton getInstance()
{
return SingletonFactory.instance;
}
}
package cn.cnic.lifx.singleton;
public class Singleton2
{
private Singleton2(){ }
private static Singleton2 instance = new Singleton2();
public static Singleton2 getInstance()
{
return instance;
}
}
package cn.cnic.lifx.singleton;
public class Singleton3
{
private Singleton3(){ }
private static Singleton3 instance;
public synchronized static Singleton3 getInstance()
{
if(null == instance)
{
instance = new Singleton3();
}
return instance;
}
}
上一篇: Delphi 版的 Ping
下一篇: java单例模式