2_单例模式

2020-12-23  本文已影响0人  真是个点子王

单例模式

一、饿汉式(静态常量)

public class SingletonTest01 {
    public static void main(String[] args) {
        PersonManager manager = PersonManager.getInstance();
        PersonManager manager1 = PersonManager.getInstance();
        System.out.println(manager == manager1);
        System.out.println(manager.hashCode());
        System.out.println(manager1.hashCode());
    }
}

class PersonManager{
    // 2. 创建静态成员变量
    private static final PersonManager INSTANCE = new PersonManager();
    // 1. 将构造器私有化
    private PersonManager(){
    }
    // 3.提供一个静态方法,返回实例对象
    public static PersonManager getInstance() {
        return INSTANCE;
    }
}

优缺点:

二、饿汉式(静态代码块)

public class SingletonTest02 {
    public static void main(String[] args) {
        PersonManager02 manager = PersonManager02.getInstance();
        PersonManager02 manager1 = PersonManager02.getInstance();
        System.out.println(manager == manager1);
        System.out.println(manager.hashCode());
        System.out.println(manager1.hashCode());
    }
}

class PersonManager02{

    private static final PersonManager02 INSTANCE;

    static {
        INSTANCE = new PersonManager02();
    }
    private PersonManager02(){

    }

    public static PersonManager02 getInstance(){
        return INSTANCE;
    }

三、懒汉式(线程不安全)

public class Singleton03 {
    public static void main(String[] args) {
        PersonManager03 manager = PersonManager03.getInstance();
        PersonManager03 manager1 = PersonManager03.getInstance();
        System.out.println(manager == manager1);
        System.out.println(manager.hashCode());
        System.out.println(manager1.hashCode());
    }

}

class PersonManager03{
    private static PersonManager03 instance;
    private PersonManager03(){
    }
    public static PersonManager03 getInstance(){
        if(instance == null){
            instance = new PersonManager03();
        }
        return instance;
    }
}

四、懒汉式(线程安全)

public class Singleton04 {
    public static void main(String[] args) {
        PersonManager04 manager = PersonManager04.getInstance();
        PersonManager04 manager1 = PersonManager04.getInstance();
        System.out.println(manager == manager1);
        System.out.println(manager.hashCode());
        System.out.println(manager1.hashCode());
    }
}

class PersonManager04{
    private static PersonManager04 instance;
    private PersonManager04(){
    }
    public static synchronized PersonManager04 getInstance(){
        if(instance == null){
            instance = new PersonManager04();
        }
        return instance;
    }
}

五、Double Check

public class Singleton05 {
    public static void main(String[] args) {
        PersonManager05 manager = PersonManager05.getInstance();
        PersonManager05 manager1 = PersonManager05.getInstance();
        System.out.println(manager == manager1);
        System.out.println(manager.hashCode());
        System.out.println(manager1.hashCode());
    }

}

class PersonManager05{

    private static volatile PersonManager05 instance;
    private PersonManager05(){
    }
    public static PersonManager05 getInstance(){
        if(instance == null){
            synchronized (PersonManager05.class){
                if(instance == null) {
                    instance = new PersonManager05();
                }
            }

        }
        return instance;
    }
}

六、静态内部类

public class SingletonTest06 {
    public static void main(String[] args) {
        PersonManager05 manager = PersonManager05.getInstance();
        PersonManager05 manager1 = PersonManager05.getInstance();
        System.out.println(manager == manager1);
        System.out.println(manager.hashCode());
        System.out.println(manager1.hashCode());
    }
}

class PersonManager06 {
 
    // 1、私有化构造器
    private PersonManager06(){
    }

    // 2、写一个静态内部类,该类中有一个静态属性Person
    private static class PersonInstance{
        private static final PersonManager06 INSTANCE = new PersonManager06();
    }

    // 3、提供一个静态的公有方法,直接返回Person.INSTANCE
    public static PersonManager06 getInstance(){
        return PersonInstance.INSTANCE;
    }
}

七、枚举

public class SingletonTest07 {
    public static void main(String[] args) {
        PersonManager07 manager = PersonManager07.INSTANCE;
        PersonManager07 manager1 = PersonManager07.INSTANCE;
        System.out.println(manager == manager1);
        System.out.println(manager.hashCode());
        System.out.println(manager1.hashCode());
        manager.sayOK();
    }
}

enum PersonManager07{
    INSTANCE;
    public void sayOK(){
        System.out.println("ok");
    }
}

实例

上一篇下一篇

猜你喜欢

热点阅读