第一章《设计模式-单例模式》

1.延迟加载模式

package test.my.maven.DesignModel;

/**
 * 延迟加载模式
 * 模式: 内部类模式
 * 优点: 延迟加载 && 线程安全 && 无锁 && 执行效率高
 * 解决: 避免一个全局对象频繁创建、销毁   
 */
public class SingleModel {

    /**
     * 利用类加载机制的特点
     * 通过内部类实现单例模式
     * 延迟加载,减少内存开销
     */
    private static class SingletonHolder{
        // 你不代码快和私有构造方法 都不会执行
//        private SingletonHolder(){
//            System.out.println("SingletonHolder()");
//        }
//        {
//            System.out.println("SingletonHolder");
//        }
        private static SingleModel singleModel = new SingleModel();
    }

    /**
     * 构造函数私有化
     */
    private  SingleModel(){
        // 仅调用一次
        System.out.println("构造函数");
    }

    public static SingleModel getInstance(){
        return SingletonHolder.singleModel;
    }

//    public static void testMethod(){
//        System.out.println("内部类实现单例模式");
//    }

    public static void main(String[] args) {
//        SingleModel.getInstance().testMethod();
        for(int i = 0; i<10;i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+":"+SingleModel.getInstance());
                }
            }).start();

        }
    }
}

2.非延迟加载模式

package test.my.maven.DesignModel;

/**
 * 非延迟加载
 * 线程安全
 * 类加载时,浪费内存
 * 无锁,执行效率高
 */
public class NoLazingModel {

    /**
     * 类加载时,会实例化。消耗内存
     */
    private static NoLazingModel noLazingModel = new NoLazingModel();

    private NoLazingModel(){
        System.out.println("非延迟加载");
    }
    public static NoLazingModel getInstance(){
        return noLazingModel;
    }

    public static void main(String[] args) {
        for(int i = 0; i<10;i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+":"+NoLazingModel.getInstance());
                }
            }).start();

        }
    }
}

3. 枚举创建实例

package test.my.maven.DesignModel;

/**
 * 这种是实现单例模式的最佳方法。它更简洁,自动支持序列化机制,绝对防止多次实例化。
 */
public class EnumTest {

    private EnumTest(){
        System.out.println("EnumTest 构造函数");
    }

    public static EnumTest getInstance(){
        return EnumSingleton.INSTANCE.getInstance();
    }

    private static enum EnumSingleton{
        INSTANCE;
        private EnumTest singleton;

        //JVM会保证此方法绝对只调用一次
        private EnumSingleton(){
            System.out.println("EnumSingleton 构造函数");
            singleton = new EnumTest();
        }
        public EnumTest getInstance(){
            return singleton;
        }
    }

    public static void main(String[] args) {
        for(int i = 0; i<10;i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+":"+ EnumTest.getInstance());
                }
            }).start();

        }
    }

}

  

posted @ 2018-03-11 11:25  聊聊IT那些事  阅读(79)  评论(0)    收藏  举报