单例模式:一个类只会生成唯一的一个对象
public class SingpatternTest
{
public static void main(String[] args)
{
Singpattern singpattern1 = singpattern.getInstance();
Singpattern singpattern2 = singpattern.getInstance();
system.out.println(singpattern1 == singpattern2);
}
}
class Singpattern
{
private static Singpattern singpattern = new Singpattern();
private Singpattern()
{
}
public static Singpattern getInstance()
{
return new Singleton();
}
}
public class SingpatternTest
{
public static void main(String[] args)
{
Singpattern singpattern1 = singpattern.getInstance();
Singpattern singpattern2 = singpattern.getInstance();
system.out.println(singpattern1 == singpattern2);
}
}
class Singpattern
{
private static Singpattern singpattern;
private Singpattern()
{
}
public static Singpattern getInstance()
{
if(singpattern = null)
{
singpattern = new Singpattern();
}
return new Singleton();
}
}
网友评论