java中单例模式

来源:互联网 发布:js在win7下不能运行 编辑:程序博客网 时间:2024/06/11 03:26

java中所说的单利模式依我的方式来说呢就是私有化构造方法,然后提供一个静态的方法,自己实例化自己,然后静态方法返回这个对象出去。代码如下:

package com.zking.utils;public class Test {    //私有化构造方法之后,只能自己实例化自己。    static Test test=null;    //私有化构造方法    private Test(){    }    //提供一个静态方法    public static Test simple(){        if(test==null){             Test test1=new Test();            test=test1;        }        return test;    }}