zoukankan      html  css  js  c++  java
  • 单例模式

    单例模式:使类只有一个实例对象的设计模式称为单例模式。

    单例模式作用:1)节省内存空间 2)业务需求(有些类必须只有一个实例)

    单例的实现的几个步骤:

    1)构造方法私有化(别人不能通过new来创建这个对象)

    2)在类的内部创建单例对象

    3)通过public方法提供这个单例对象

    单例模式的实现方式:

    1)饿汉模式(简单);

    package singleton;
    
    /**
     * 饿汉模式
     */
    public class Test1 {
        //2、创建唯一的对象
        private static final Test1 test1=new Test1();
        /**
         * 1、构造方法私有化
         */
        private Test1(){
    
        }
        //3、写一个public方法提供该对象
        public static Test1 getInstance(){
            return test1;
        }
        public void test(){
            System.out.println("test方法");
        }
    }

    2)懒汉模式:使用的时候再去创建对象;

    package singleton;
    
    /**
     * 懒汉模式
     */
    public class Test2 {
        //2、创建唯一的对象
        private static Test2 test2;
        /**
         * 1、构造方法私有化
         */
        private Test2(){
    
        }
        //3、懒汉模式,在调用getInstance方法时,才会去创建这个对象
        public static synchronized Test2 getInstance(){
            if (test2==null){
                test2= new Test2();
            }
            return test2;
        }
        public void test(){
            System.out.println("test方法");
        }
    }

    3)双重检查锁定:解决懒汉模式性能问题;

    package singleton;
    /**
     *  双重检查锁定
     * @author bigpeng
     * @create 2020-06-28-17:36
     */
    public class Test3 {
        // 2.创建唯一的对象
        // volatile 关键字作用 :使对象的变化对其他的线程可见。
        private volatile static Test3 test3;
    
        /**
         * 1.构造方法私有化
         */
        private Test3(){
    
        }
        /**
         * 3、双重检查锁定,在调用getInstance方法时,才去创建这个对象
         * @return
         */
        public static  Test3 getInstance(){
            if(test3 ==null){//只有第一次并发访问时,能进入if
                synchronized (Test3.class) {
                    if(test3==null) {
                        test3 = new Test3();
                    }
                }
            }
            return test3;
        }
        public void test(){
            System.out.println("test方法");
        }
    
    }

    4)枚举(推荐);

    package singleton;
    
    /**
     * 枚举类型实现单例,不能被反射创建和反序列化创建
     * @author bigpeng
     * @create 2020-06-28-18:05
     */
    public enum Test4 {
        TEST4;//定义唯一的实例对象
        private Test4(){
        }
        public void test(){
            System.out.println("枚举单例的方法");
        }
    }
    package singleton;
    
    public class Test {
        public static void main(String[] args) {
            Test1 instance = Test1.getInstance();
            instance.test();
            Test1 instance2 = Test1.getInstance();
            System.out.println(instance==instance2);//true
    
            //获取枚举对象
            Test4 test4 = Test4.TEST4;
            test4.test();
        }
    }
  • 相关阅读:
    React Native 架构演进
    React Native 架构一览
    React Native 在 Airbnb 的起起落落
    React Native简史
    图解云服务模型的演进
    伯克利研究员们眼中的Cloud Computing
    彻底理解 IaaS、PaaS、SaaS
    JS更随机的随机数
    JS自动化
    fingerprint2 计算浏览器指纹分析
  • 原文地址:https://www.cnblogs.com/xie-qi/p/13205658.html
Copyright © 2011-2022 走看看