public class EagerSingleton
{
private static final EagerSingleton m_instance =
new EagerSingleton();
/**
* 私有的默认构造子
*/
private EagerSingleton() { }
/**
* 静态工厂方法
*/
public static EagerSingleton getInstance()
{return m_instance;
}
}
{
private static final EagerSingleton m_instance =
new EagerSingleton();
/**
* 私有的默认构造子
*/
private EagerSingleton() { }
/**
* 静态工厂方法
*/
public static EagerSingleton getInstance()
{return m_instance;
}
}
{
private LazySingleton() { } synchronized public static LazySingleton getInstance()
{
if (m_instance == null)
{
m_instance = new LazySingleton();
}
return m_instance;
} /**
* @label Creates
*/
private static LazySingleton m_instance = null;
}
{
private OneInc()
{ }
static OneInc newInc()
{
return new OneInc();
}
}
应该是
class OneInc
{
private OneInc()
{ }
public static OneInc newInc()
{
return new OneInc();
}
}
{
private static A instanceA; private A() {} public static A getInstance()
{
if (instanceA == null)
{
instanceA = new A();
}
return instanceA;
}
}
用final定義吧。
单子模式结构