设计模式-单例模式-饿汉单例与懒汉单例.

package 单例模式;

public class HungerSingleton {

    public static void main(String[] args) {

        HungerPunker hp = HungerPunker.getHP();
        HungerPunker hp1 = HungerPunker.getHP();
        System.out.println(hp);
        System.out.println(hp1);
    }

}


//another class
class HungerPunker{

   private HungerPunker(){

}
private static final HungerPunker hp = new HungerPunker(); public static HungerPunker getHP() { return hp; } }

 

 

package 单例模式;

public class LazySingleton {

    public static void main(String[] args) {

        // get the Lazy! instance!
        LazyPunker lp = LazyPunker.getLP();
        LazyPunker lp1 = LazyPunker.getLP();
        System.out.println(lp);
        System.out.println(lp1);
    }

}

// another class the LazyPunker~
class LazyPunker {

private LazyPunker(){

}
private static LazyPunker lp; public synchronized static LazyPunker getLP() { if (lp == null) { lp = new LazyPunker(); } return lp; } }

 

最近看<重构>这本书,原因是觉得代码打到一定程度之后会变得臃肿复杂,难以理解,如果没有一种好的设计模式,好的规范来框起来,

难免会变得没法去查看,更别说维护了.

关于设计模式还有很多书.

这次先贴上饿汉与懒汉的单例模式.

不知道哪位大仙取的这个名字...

将编程看作是一门艺术,而不单单是个技术。 敲打的英文字符是我的黑白琴键, 思维图纸画出的是我编写的五线谱。 当美妙的华章响起,现实通往二进制的大门即将被打开。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值