设计模式-单件模式

单件模式,就是确保一个类只有一个实例,并提供一个全局访问点。

两点:访问点唯一,实例只New一次

单件常常被用来管理共享资源,例如数据库连接或者线程池。

    class Singleton
    {
        private static Singleton uniqueInstance;//单一实例

        public static Singleton getInstance()
        {
            if (uniqueInstance == null)//保证只是实例化一次
            {
                uniqueInstance = new Singleton();
            }
            return uniqueInstance;
        }
    }

posted @ 2010-03-15 00:20  翱翔之鹰  阅读(124)  评论(0编辑  收藏  举报