java23中设计模式之单例模式

上一节说到了建造者模式:https://blog.csdn.net/zhanglei082319/article/details/88363894

所谓的建造者模式,就是构建一个负责管理实现某种产品的全部生产方案,最后得到完整的产品。

     今天我们来说一下另一种设计模式:单例模式

     还是围绕着上一个话题,手机工厂的问题。我们知道一个车间工厂每天会生产出很多手机零件以及手机成品,同时还会报废许多不合格的产品。那么现在有个需求,我们需要记录工厂每天针对于苹果手机的成品生产数量,半成品生产数量,以及报废数量。

      为了解决这个问题,工厂可能会专门请一个负责记录的观察员,或者检验员来负责。但是一个车间很大,有很多生产线,一个人根本无法光顾下来。怎么办?这里就需要引入一种新的设计模式:  单例模式。

      单例模式概念:保证一个类有且仅有一个实例,并且提供一个访问它的全局访问点。

      我们可以在车间指定的地点建造一个单一的产品过滤管道。不管是报废的产品,成品,半成品都需要在这个地方进行登记。

      具体代码如下:

   1、饿汉模式:

public class Record{
     //成品数量
     private AtomicInteger finishProductCount = new AtomicInteger();
     //半成品数量
     private AtomicInteger parPerProductCount = new AtomicInteger();
     //报废品数量
     private AtomicInteger scrapCount = new AtomicInteger();
     private Record(){}
     private static  Record  record = new Record();
     public static Record getInstance(){
          return Record.record;
     }
     public int addFinish(){
          return this.finishProductCount.incrementAndGet();
     }
     public int delFinish(){
          return this.finishProductCount.decrementAndGet();
     }
     public int nowFinish(){
          return this.finishProductCount.get();
     }
     
     public int addParPerProductCount(){
          return this.parPerProductCount.incrementAndGet();
     }
     
     public int delParPerProductCount(){
          return this.parPerProductCount.decrementAndGet();
     }
     
     public int nowParPerProductCount(){
          return this.parPerProductCount.get();
     }
     
     
     public int addScrapCount(){
          return this.scrapCount.incrementAndGet();
     }
     
     public int delScrapCount(){
          return this.scrapCount.decrementAndGet();
     }
     
     public int nowScrapCount(){
          return this.scrapCount.get();
     }
}

      饿汉模式,是指单例类实例只要单例类本身被调用,实例就会被创建。而并不会在调用全局单例访问点时创建。

   2、懒汉模式:

public class Record{
   
     private static volatile  Record  record ;

     private Record(){}
     
     public static Record getInstance(){
          //双重判断 + volatile 实现线程安全
          if(record==null){
               synchronized (Record.class){
                    if(record==null){
                         record = new Record();
                    }
               }
          }
          return record;
     }

}

     这里为了观看更加清晰明了,将多余的业务数据删除。

     懒汉模式,指的是只有在调用单例全局调用方法时,才会查找或者创建系统中唯一的单例。这样做的好处可以节省系统资源。

    3、使用静态内部类实现的懒汉模式

public static class Record{
    
     private Record(){}

     private static class LoaderInstance{
          private static Record record = new Record();
     }

     public static Record getInstance(){
          return LoaderInstance.record;
     }
     
}

     将类的实例化放置到内部类中,我们知道静态内部类和静态变量不一样,静态内部类是不会在外部类被调用时初始化的。只有在我们调用了静态内部类本身,它才会被初始化。所以我们通过这种方式实现懒汉模式,即保证了线程的安全性,又提高了程序的访问性能,最后也节约了系统资源。

      总结一下:单例模式,一般比较推荐使用懒汉模式,在懒汉模式中又比较推荐使用静态内部类实现的懒汉模式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值