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

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

原文地址:https://www.cnblogs.com/yimengyizhen/p/11166940.html