美文网首页
设计模式

设计模式

作者: Lutecium | 来源:发表于2018-03-21 21:36 被阅读0次

单例模式

  • 确保全局之多一个对象
  • 用于构造缓慢的对象,需要统一管理的资源
  • 缺点是很多全局状态,线程安全性
/**
 * 懒汉模式
 * 单例的实例在第一次使用时创建
 * 线程不安全
 */
public class SingletonExample1 {

    //私有构造函数
    private SingletonExample1(){

    }

    //单例对象
    private static SingletonExample1 instance = null;

    //静态的工厂方法
    public static SingletonExample1 getInstance(){
        if(instance == null){
            instance = new SingletonExample1();
        }
        return instance;
    }
}

/**
 * 懒汉模式
 * 单例的实例在第一次使用时创建
 * 线程安全
 */
public class SingletonExample3 {

    //私有构造函数
    private SingletonExample3(){

    }

    //单例对象
    private static SingletonExample3 instance = null;

    //静态的工厂方法
    public static synchronized SingletonExample3 getInstance(){
        if(instance == null){
            instance = new SingletonExample3();
        }
        return instance;
    }
}

/**
 * 懒汉模式 --- 双重同步锁单例模式
 * 单例的实例在第一次使用时创建
 * 线程不安全
 */
public class SingletonExample4 {

    //私有构造函数
    private SingletonExample4(){

    }
    // 1、memory = allocate() 分配内存空间
    // 2、ctorInstance() 初始化对象
    // 3、instance = memory 设置instance指向刚分配的内存

    //JVM和cpu优化,发生了指令重排

    // 1、memory = allocate() 分配内存空间
    // 3、instance = memory 设置instance指向刚分配的内存
    // 2、ctorInstance() 初始化对象

    //单例对象
    private static SingletonExample4 instance = null;

    //静态的工厂方法
    public static SingletonExample4 getInstance(){
        if(instance == null){ //双重检测机制
            synchronized(SingletonExample4.class){//同步锁
                if(instance == null) {
                    instance = new SingletonExample4();
                }
            }
        }
        return instance;
    }
}

/**
 * 懒汉模式 --- 双重同步锁单例模式
 * 单例的实例在第一次使用时创建
 * 线程安全
 */
public class SingletonExample5 {

    //私有构造函数
    private SingletonExample5(){

    }
    // 1、memory = allocate() 分配内存空间
    // 2、ctorInstance() 初始化对象
    // 3、instance = memory 设置instance指向刚分配的内存

    //JVM和cpu优化,发生了指令重排

    // 1、memory = allocate() 分配内存空间
    // 3、instance = memory 设置instance指向刚分配的内存
    // 2、ctorInstance() 初始化对象

    //单例对象 volatile + 双重检测机制 -> 禁止指令重排
    private volatile static SingletonExample5 instance = null;

    //静态的工厂方法
    public static SingletonExample5 getInstance(){
        if(instance == null){ //双重检测机制
            synchronized(SingletonExample5.class){//同步锁
                if(instance == null) {
                    instance = new SingletonExample5();
                }
            }
        }
        return instance;
    }
}



/**
 * 饿汉模式
 * 单例的实例在类装载时创建
 * 线程安全
 */
public class SingletonExample2 {

    //私有构造函数
    private SingletonExample2(){

    }
    //单例对象
    private static SingletonExample2 instance = new SingletonExample2();

    //静态的工厂方法
    public static SingletonExample2 getInstance(){
        return instance;
    }

}
/**
 * 饿汉模式
 * 单例的实例在类装载时创建
 * 线程安全
 */
public class SingletonExample6 {

    //私有构造函数
    private SingletonExample6(){

    }
    //单例对象
    private static SingletonExample6 instance = null;

    static{
        instance = new SingletonExample6();
    }

    //静态的工厂方法
    public static SingletonExample6 getInstance(){
        return instance;
    }

    public static void main(String[] args) {
        System.out.println(getInstance().hashCode());
        System.out.println(getInstance().hashCode());
    }
}

/**
 * 枚举模式:最安全,推荐使用
 * @author WangCH
 * @create 2018-03-21 21:23
 */
public class SingletonExample7 {

    private SingletonExample7(){

    }

    public static SingletonExample7 getInstance(){
        return Singleton.INSTANCE.getInstance();
    }

    private enum Singleton{

        INSTANCE;

        private SingletonExample7 singletonExample7;

        //JVM保证绝对只被调用1次
        Singleton(){
            singletonExample7 = new SingletonExample7();
        }

        public SingletonExample7 getInstance() {
            return singletonExample7;
        }
    }
}

相关文章

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

  • 设计模式笔记汇总

    目录 设计原则 “依赖倒置”原则 未完待续... 设计模式 设计模式——策略模式 设计模式——装饰者模式 设计模式...

  • 设计模式

    《C#设计模式》 《C#设计模式》-设计模式概述 《C#设计模式》-面向对象设计原则 《C#设计模式》-单例模式 ...

  • 浅谈JS的一些设计模式

    @(书籍阅读)[JavaScript, 设计模式] 常见设计模式 设计模式简介 设计模式概念解读 设计模式的发展与...

  • 前端设计模式

    JS设计模式一:工厂模式jS设计模式二:单例模式JS设计模式三:模块模式JS设计模式四:代理模式JS设计模式五:职...

  • 设计模式之工厂模式

    设计模式之工厂模式 标签(空格分隔): 设计模式 工厂模式 设计模式的感念 设计模式的应用 工厂设计模式的产生 工...

  • JavaJavascript基础进阶(十七)JS中常用的设计模式

    单利设计模式、构造原型设计模式、发布订阅设计模式、promise设计模式 单利模式 构造原型设计模式 最贴近OOP...

  • 设计模式 - 目录

    设计模式01 - 单例模式 设计模式02 - 工厂模式 设计模式03 - 建造者模式 设计模式04 - 适配器模式...

  • 第1章 设计模式概述

    一、设计模式的概念 二、设计模式的历史 三、设计模式的要素 四、设计模式的分类 ■ 创建型设计模式 ■ 结构型设计...

  • iOS设计模式(3)适配器模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(4)抽象工...

网友评论

      本文标题:设计模式

      本文链接:https://www.haomeiwen.com/subject/mcxgfftx.html