zoukankan      html  css  js  c++  java
  • 设计模式之单例模式的几种写法——java

    对于设计模式的使用场景和好处,之前有介绍一篇,今天主要是单例模式的编写方式,直接看代码吧

    • 单例模式之饿汉模式,不会懒加载。线程安全

    /**
     * @Author wangtao
     * @Description  单例模式之饿汉模式,不会懒加载。线程安全
     * @Date 2019-5-22  12:32
     * @优点  线程安全,简单易实现
     * @缺点  在进行类加载的时候就创建好实例,会占用内存
     **/
    class SingletonHungry{
        //私有构造函数
        private  SingletonHungry(){}
        //创建实例
        private static final SingletonHungry singletonHungry=new SingletonHungry();
        //提供获取公有方法
        public static SingletonHungry getInstance(){
            return singletonHungry;
        }
    }
    • 单例模式之懒汉模式,会懒加载。线程不安全

    /**
     * @Author wangtao
     * @Description  单例模式之懒汉模式,会懒加载。线程不安全
     * @Date 2019-5-22  12:32
     * @优点
     * @缺点
     **/
    class SingletonLazy {
        //私有构造函数
        private SingletonLazy() {}
        //创建实例
        private static SingletonLazy singletonLazy;
        //提供获取公有方法
        public static SingletonLazy getInstance() {
            if(singletonLazy==null){
                singletonLazy=new SingletonLazy();
            }
            return singletonLazy;
        }
    }
    

      

    • 单例模式之懒汉模式,会懒加载。线程安全,同步方法

    /**
     * @Author wangtao
     * @Description  单例模式之懒汉模式,会懒加载。线程安全,同步方法
     * @Date 2019-5-22  12:32
     * @优点
     * @缺点
     **/
    class SingletonLazyThread {
        //私有构造函数
        private SingletonLazyThread() {}
        //创建实例
        private static SingletonLazyThread singletonLazyThread;
        //提供获取公有方法
        public static synchronized SingletonLazyThread getInstance() {
            if(singletonLazyThread==null){
                singletonLazyThread=new SingletonLazyThread();
            }
            return singletonLazyThread;
        }
    }
    
    • 单例模式之懒汉模式,会懒加载。线程安全,同步代码块(文本称为:双重锁)

     

    /**
     * @Author wangtao
     * @Description  单例模式之懒汉模式,会懒加载。线程安全,同步代码块(文本称为:双重锁)
     * @Date 2019-5-22  12:32
     * @优点
     * @缺点
     **/
    class SingletonLazyThread2 {
        //私有构造函数
        private SingletonLazyThread2() {}
        //创建实例
        private static SingletonLazyThread2 singletonLazyThread2;
        //提供获取公有方法
        public static  SingletonLazyThread2 getInstance() {
            synchronized(SingletonLazyThread2.class){
                if(singletonLazyThread2==null){
                    singletonLazyThread2=new SingletonLazyThread2();
                }
            }
            return singletonLazyThread2;
        }
    }
    • 单例模式之饿汉模式优化,会懒加载。线程安全,使用静态内部类

    /**
     * @Author wangtao
     * @Description  单例模式之饿汉模式优化,会懒加载。线程安全,使用静态内部类
     * @Date 2019-5-22  12:32
     * @优点 懒加载,在进行加载内部类的时候才会初始化对象,线程安全,是饿汉模式的优化,避免了直接的实例化占用内存空间的问题
     * @缺点 只使用于静态的方法
     **/
    class SingletonInnerClass{
        //私有构造函数
        private SingletonInnerClass() {}
        //创建实例
        private static class SingletonInner{
            private static final  SingletonInnerClass singletonInnerClass =new SingletonInnerClass();
        }
        //提供获取公有方法
        public static  SingletonInnerClass getInstance() {
            return SingletonInner.singletonInnerClass;
        }
    }
    • 单例模式之枚举,不会懒加载。线程安全,自动序列化

    /**
     * @Author wangtao
     * @Description  单例模式之枚举,不会懒加载。线程安全,自动序列化
     * @Date 2019-5-22  12:32
     * @优点
     * @缺点
     **/
    enum  SingletonEnum{
        INSTANCE;
        public void say(){
            System.out.println("枚举类的方法");
        }
    }
    

      

  • 相关阅读:
    LeetCode 876——链表的中间结点
    LeetCode 206——反转链表
    一次漫长的代码复现经历
    在 C/C++ 中使用 TensorFlow 预训练好的模型—— 直接调用 C++ 接口实现
    编译 TensorFlow 的 C/C++ 接口
    TensorFlow 同时调用多个预训练好的模型
    在 C/C++ 中使用 TensorFlow 预训练好的模型—— 间接调用 Python 实现
    TensorFlow 调用预训练好的模型—— Python 实现
    Python 学习笔记之 Numpy 库——文件操作
    Python 学习笔记之 Numpy 库——数组基础
  • 原文地址:https://www.cnblogs.com/gdhzdbh/p/11101793.html
Copyright © 2011-2022 走看看