本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7594531
建立时间:2006年5月29日




[设计模式]『蚂蚁学模式』之实例化单例模式(一)
软件技术

lhwork 发表于 2007/1/10 9:31:57

饿汉式单例com.cleversoft.designpatterns.singleton1.Singleton.java: 500)this.width=500'>package com.cleversoft.designpatterns.singleton1;500)this.width=500'>500)this.width=500'>500)this.width=500'>/** *//**500)this.width=500'> * Hungry Singleton500)this.width=500'> * 500)this.width=500'> */500)this.width=500'>500)this.width=500'>public class Singleton 500)this.width=500'>{500)this.width=500'>500)this.width=500'>    private static Singleton singleton = new Singleton();500)this.width=500'>500)this.width=500'>500)this.width=500'>    private Singleton() 500)this.width=500'>{500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    public static Singleton getInstance() 500)this.width=500'>{500)this.width=500'>        return singleton;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    public String demoMethod() 500)this.width=500'>{500)this.width=500'>        return "This is a hungry singleton demo!";500)this.width=500'>    }500)this.width=500'>}com.cleversoft.designpatterns.singleton1.Main.java: 500)this.width=500'>package com.cleversoft.designpatterns.singleton1;500)this.width=500'>500)this.width=500'>500)this.width=500'>public class Main 500)this.width=500'>{500)this.width=500'>500)this.width=500'>500)this.width=500'>    /** *//**500)this.width=500'>     * @param args500)this.width=500'>     */500)this.width=500'>500)this.width=500'>    public static void main(String[] args) 500)this.width=500'>{500)this.width=500'>        // TODO Auto-generated method stub500)this.width=500'>        System.out.println(Singleton.getInstance().demoMethod());500)this.width=500'>    }500)this.width=500'>500)this.width=500'>}懒汉式单例com.cleversoft.designpatterns.singleton2.LazySingleton.java: 500)this.width=500'>package com.cleversoft.designpatterns.singleton2;500)this.width=500'>500)this.width=500'>500)this.width=500'>/** *//**500)this.width=500'> * Lazy Singleton500)this.width=500'> * 500)this.width=500'> */500)this.width=500'>500)this.width=500'>public class LazySingleton 500)this.width=500'>{500)this.width=500'>    static boolean instance_flag = false;500)this.width=500'>500)this.width=500'>500)this.width=500'>    private LazySingleton() 500)this.width=500'>{500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    synchronized public static LazySingleton getInstance() 500)this.width=500'>{500)this.width=500'>500)this.width=500'>        if (!instance_flag) 500)this.width=500'>{500)this.width=500'>            instance_flag = true;500)this.width=500'>            return new LazySingleton();500)this.width=500'>        } else500)this.width=500'>            return null;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>}com.cleversoft.designpatterns.singleton2.Main.java: 500)this.width=500'>package com.cleversoft.designpatterns.singleton2;500)this.width=500'>500)this.width=500'>500)this.width=500'>public class Main 500)this.width=500'>{500)this.width=500'>500)this.width=500'>500)this.width=500'>    /** *//**500)this.width=500'>     * @param args500)this.width=500'>     */500)this.width=500'>500)this.width=500'>    public static void main(String[] args) 500)this.width=500'>{500)this.width=500'>        // TODO Auto-generated method stub500)this.width=500'>        LazySingleton ls1, ls2;500)this.width=500'>        System.out.println("Start to get first Instance500)this.width=500'>");500)this.width=500'>        ls1 = LazySingleton.getInstance();500)this.width=500'>500)this.width=500'>        if (ls1 != null)500)this.width=500'>{500)this.width=500'>            System.out.println("This is the first Instance!\n");500)this.width=500'>        }500)this.width=500'>        500)this.width=500'>        System.out.println("Start to get second Instance500)this.width=500'>");500)this.width=500'>        ls2 = LazySingleton.getInstance();500)this.width=500'>500)this.width=500'>        if (ls2 != null)500)this.width=500'>{500)this.width=500'>            System.out.println("This is the second Instance!");500)this.width=500'>        }500)this.width=500'>500)this.width=500'>        else500)this.width=500'>{            500)this.width=500'>            System.out.println("You can just get 1 Instance!");500)this.width=500'>        }500)this.width=500'>        500)this.width=500'>    }500)this.width=500'>500)this.width=500'>}引用: >强烈建议大家不要再花时间在实现双重检查成例上是这样,不必在这上面钻牛角尖,而且单例模式在实际应用中已经逐步被Ioc容器替代,实用价值不高。(banq)(http://www.jdon.com/jive/thread.jsp?forum=91&thread=23890)参考资料:1. 《Java与模式》2. http://www.java2s.com


阅读全文(2732) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.375 second(s), page refreshed 144783969 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号