Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1503987
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 20:53:13

一起学习

(2)懒汉式单例类

与饿汉式单例类不同的是,懒汉式单例类在第一次被引用时将自己实例化。

Java代码:

public class Singleton {      

    public static Singleton theInstance = null;      

    private Singleton(){}      

    public synchronized static Singleton instance(){     

        if(theInstance == null){     

            return new Singleton();     

        }     

        return theInstance;     

    }      

}    

   



public class Singleton {   

    public static Singleton theInstance = null;   

    private Singleton(){}   

    public synchronized static Singleton instance(){  

        if(theInstance == null){  

            return new Singleton();  

        }  

        return theInstance;  

    }   

}

(3)登记式单例类

登记式单例类是GoF 为了克服饿汉式单例类及懒汉式单例类均不可继承的缺点而设计的。

Java代码:

public class RegSingleton     

{     

    static private HashMap m_registry = new HashMap();     

    static     

    {     

        RegSingleton x = new RegSingleton();     

        m_registry.put(x.getClass().getName(), x);     

    }      

    protected RegSingleton(){      

    }      

    static public RegSingleton getInstance(String name)     

    {     

        if (name == null)     

        {     

            name = "com.javapatterns.singleton.demos.RegSingleton";     

        }     

        if (m_registry.get(name) == null)     

        {     

            try     

            {     

                m_registry.put(name, Class.forName(name).newInstance());     

            }     

            catch (Exception e)     

            {     

                System.out.println("Error happened.");     

            }     

        }     

        return (RegSingleton) (m_registry.get(name));     

    }      

}  



public class RegSingleton  

{  

    static private HashMap m_registry = new HashMap();  

    static  

    {  

        RegSingleton x = new RegSingleton();  

        m_registry.put(x.getClass().getName(), x);  

    }   

    protected RegSingleton(){   

    }   

    static public RegSingleton getInstance(String name)  

    {  

        if (name == null)  

        {  

            name = "com.javapatterns.singleton.demos.RegSingleton";  

        }  

        if (m_registry.get(name) == null)  

        {  

            try  

            {  

                m_registry.put(name, Class.forName(name).newInstance());  

            }  

            catch (Exception e)  

            {  

                System.out.println("Error happened.");  

            }  

        }  

        return (RegSingleton) (m_registry.get(name));  

    }   

}

登记式单例类的子类

Java代码:

import java.util.HashMap;   

public class RegSingletonChild extends RegSingleton   

{   

public RegSingletonChild() {}   

static public RegSingletonChild getInstance()   

{   

return (RegSingletonChild)   

RegSingleton.getInstance(   

"com.javapatterns.singleton.demos.RegSingletonChild" );   

}   

  

public String about()   

{   

return "Hello, I am RegSingletonChild.";   

}   

}

TAG: 设计模式

下载本文示例代码


讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)讲解JAVA设计模式中的单例模式 (2)
阅读(128) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~