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();
        }
    }
  • 相关阅读:
    array_map()与array_shift()搭配使用 PK array_column()函数
    Educational Codeforces Round 8 D. Magic Numbers
    hdu 1171 Big Event in HDU
    hdu 2844 poj 1742 Coins
    hdu 3591 The trouble of Xiaoqian
    hdu 2079 选课时间
    hdu 2191 珍惜现在,感恩生活 多重背包入门题
    hdu 5429 Geometric Progression 高精度浮点数(java版本)
    【BZOJ】1002: [FJOI2007]轮状病毒 递推+高精度
    hdu::1002 A + B Problem II
  • 原文地址:https://www.cnblogs.com/xie-qi/p/13205658.html
Copyright © 2011-2022 走看看