• java设计模式--单例模式


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

    当我们需要一个类只有一个实例时,我们就可以使用单例模式,单例模式分为两种,懒汉式单例和饿汉式单例。首先我们看懒汉式单例

    public class SuperMe {
    
        /**
         * volatile 确保superMe在线程中同步
         */
        private static volatile SuperMe superMe = null;
    
        /**
         * private避免类在外部被实例化
         */
        private SuperMe(){
            System.out.println("I'm the superMe");
        }
    
        public static synchronized SuperMe getInstance(){
            if(superMe == null){
                superMe = new SuperMe();
            }else {
                System.out.println("No second allowed");
            }
            return superMe;
        }
    
        public void getName(){
            System.out.println("Hitler");
        }
    }

    测试类

    public class LazySingleton {
        public static void main(String[] args) {
            SuperMe superMe = SuperMe.getInstance();
            superMe.getName();
            SuperMe superMe2= SuperMe.getInstance();
            superMe2.getName();
            if(superMe ==superMe2){
                System.out.println("Hitler is a dictator");
            }else {
                System.out.println("they are not same");
            }
        }
    }

    测试结果:

    I'm the superMe
    Hitler
    No second allowed
    Hitler
    Hitler is a dictator

    懒汉式单例的特点是类加载时没有生成实例,只有当第一次调用getInstance()方法时才会去创建这个单例。

    饿汉式单例的例子如下:

    public class HungrySuperMe {
        private static HungrySuperMe hungrySuperMe = new HungrySuperMe();
    
        private HungrySuperMe() {
            System.out.println("I'm the hungrySuperMe");
        }
    
        public static HungrySuperMe getHungrySuperMe() {
            return hungrySuperMe;
        }
    }

    测试类

    public class HungrySingleton {
    
        public static void main(String[] args) {
            HungrySuperMe hungrySuperMe = HungrySuperMe.getHungrySuperMe();
            HungrySuperMe hungrySuperMe2 = HungrySuperMe.getHungrySuperMe();
            if(hungrySuperMe == hungrySuperMe2){
                System.out.println("I'm the only one");
            }else {
                System.out.println("I'm not at the top");
            }
        }
    }

    测试结果:

    I'm the hungrySuperMe
    I'm the only one

     饿汉式单例在类创建的同时就已经创建好一个静态的对象供系统使用,以后不再改变,所以是线程安全的,可以直接用于多线程而不会出现问题。

  • 相关阅读:
    iOS图片压缩上传
    Spring MVC获得HttpServletRequest
    BZOJ 1002 FJOI2007 轮状病毒 递推+高精度
    破解2559
    Redis源代码分析(十七)--- multi事务操作
    Android setTag()与getTag(),与set多个setTag()
    【NOI2015】【程序自己主动分析】【并查集+离散化】
    BZOJ1433 [ZJOI2009]假期的宿舍
    将參数从PHP传递到JavaScript中
    NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(二)
  • 原文地址:https://www.cnblogs.com/yimengyizhen/p/11166940.html
Copyright © 2020-2023  润新知