单例模式

方式一:

public class Dog{

    private static Dog d = null;

    private Dog(){}

    public static Dog getDog(){

        if(d==null){

            d = new Dog();

        }

        return d;

    }

}

方式二:

public class cat{

    private final static Cat CAT = new Cat();

    private Cat(){}

    public static Cat getCat(){

        return CAT;

    }

}

你可能感兴趣的:(单例模式)