ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

设计模式之单例模式

2021-10-08 23:35:28  阅读:152  来源: 互联网

标签:Singleton singleton 模式 System 单例 println 设计模式 hashCode out


设计模式类型

设计模式分为3种类型,共23种

1.创建型模式:单例模式、抽象工厂模式、原型模式、建造者模式、工厂模式
2.结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式
3.行为型模式:模板方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter模式)、状态模式、策略模式、职责链模式(责任链模式)

单例模式

饿汉式

饿汉式应用实例

1.构造器私有化(防止new)
2.类的内部创建对象
3.向外暴露一个静态的公共方法

代码实现

推荐使用

package com.cedric.singleton.type01;

public class SingletonTest01 {
    public static void main(String[] args) {
        // 测试
        Singleton singleton = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton == singleton2); //true

        System.out.println("singleton.hashCode()" + singleton.hashCode());
        System.out.println("singleton2.hashCode()" + singleton2.hashCode());
    }
}

/**
 * 优缺点说明:
 *  优点:这种写法比较简单,就是在类装载的时候就完成实例化,避免了线程同步问题
 *  缺点:在类装载的时候就完成实例化,没有达到lazy loading的效果,如果从始至终
 *       都没有使用过这个实例,则会造成内存的浪费
 */

// 饿汉式(静态变量)
class Singleton{

    // 1.构造器私有化,外部不能new
    private Singleton(){

    }

    // 2.本类内部创建对象实例
    private final static Singleton INSTANCE = new Singleton();

    // 3.提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance(){
        return  INSTANCE;
    }
}
package com.cedric.singleton.type02;

public class SingletonTest02 {
    public static void main(String[] args) {
        // 测试
        Singleton singleton = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton == singleton2); //true

        System.out.println("singleton.hashCode()" + singleton.hashCode());
        System.out.println("singleton2.hashCode()" + singleton2.hashCode());
    }
}

/**
 * 优缺点说明:
 *  这种方式与上一种方式类似,只不过将实例化的过程放在了静态代码块中,也是在
 *  类装载的时候执行静态代码块中的代码,初始化类的实例,优缺点同上
 */

// 饿汉式(静态变量)
class Singleton{

    // 1.构造器私有化,外部不能new
    private Singleton(){

    }

    // 2.本类内部创建对象实例
    private  static Singleton INSTANCE ;

    // 在静态代码块执行时,创建单例对象
    static{
        INSTANCE = new Singleton();
    }
    // 3.提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance(){
        return  INSTANCE;
    }
}
package com.cedric.singleton.type03;

public class SingletonTest03 {
    public static void main(String[] args) {
        // 测试
        System.out.println("懒汉式1,线程不安全");
       Singleton singleton = Singleton.getInstance();
       Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton == singleton2); //true

        System.out.println("singleton.hashCode()" + singleton.hashCode());
        System.out.println("singleton2.hashCode()" + singleton2.hashCode());
    }
}

/**
 * 优缺点说明:
 *      1.起到了 lazy loading的效果,但是只能在单线程下使用
 *      2.如果在多线程下,一个线程进入了  if(INSTANCE == null)判断语句块,还未来得及
 *         往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例,所以在多线程
 *         环境下不可使用这种方式
 *       结论:实际开发中,不要使用这种方式
 */

class Singleton{

    private static Singleton INSTANCE;

    Singleton(){}

    // 提供一个静态的公有方法,当使用到该方法时,才去创建INSTANCE
    // 即懒汉式
    public static Singleton getInstance(){
        if(INSTANCE == null){
            INSTANCE = new Singleton();
        }
        return INSTANCE;
    }

}
package com.cedric.singleton.type04;

// 懒汉式(线程安全,同步方法)
public class SingletonTest04 {
    public static void main(String[] args) {

        System.out.println("懒汉式2  线程安全");
        Singleton singleton = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton == singleton2); //true

        System.out.println("singleton.hashCode()" + singleton.hashCode());
        System.out.println("singleton2.hashCode()" + singleton2.hashCode());
    }
}

/**
 * 优缺点说明:
 *      1.解决了线程不安全问题
 *      2.效率太低,每个线程在想获得类的实例时候,执行getInstance()方法都要进行同步
 *      而其实这个方法只执行一次实例化代码就够了,后面想获得该类实例,直接return就行
 *      方法进行同步效率太低
 *
 *      结论:在实际开发中,不推荐使用这种方式
 */

class Singleton{

    private static Singleton INSTANCE;

    private Singleton(){}

    // 提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
    public static synchronized  Singleton getInstance(){
        if(INSTANCE == null){
            INSTANCE = new Singleton();
        }
        return INSTANCE;
    }
}

推荐使用

package com.cedric.singleton.type05;

public class SingletonTest05 {
    public static void main(String[] args) {
        System.out.println("双重检查");
        Singleton singleton = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton == singleton2); //true

        System.out.println("singleton.hashCode()" + singleton.hashCode());
        System.out.println("singleton2.hashCode()" + singleton2.hashCode());
    }
}

/**
 * 优缺点说明:
 *      1.Double——Check概念是多线程开发中常使用到的,如代码所示,进行 了
 *       两次if(singleton == null)检查,这样就可以保证线程安全了
 *      2.这样,实例化代码只用执行一次,后面再次访问时,判断if(singleton == null),
 *        直接return实例化对象,也避免反复进行方法同步
 *      3.线程安全  延迟加载   效率较高
 *      4.结论:在实际开发中,推荐使用这种单例设计模式
 *
 */
class Singleton{

    private static volatile Singleton singleton;
    private Singleton(){}

    /**
     * 提供了一个公有方法,加入双重检测代码,解决线程安全问题,同时解决 lazy loading问题
     * 同时保证了效率
     */
    public static Singleton getInstance(){
        if(singleton == null){
            synchronized(Singleton.class){
                if(singleton == null){
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}

推荐使用

package com.cedric.singleton.type06;

public class SingletonTest06 {
    public static void main(String[] args) {
        System.out.println("使用静态内部类完成单例模式");
        Singleton singleton = Singleton.getInstance();
        Singleton singleton2 = Singleton.getInstance();
        System.out.println(singleton == singleton2); //true

        System.out.println("singleton.hashCode()" + singleton.hashCode());
        System.out.println("singleton2.hashCode()" + singleton2.hashCode());
    }
}

/**
 * 避免了线程不安全,利用静态内部类特点实现加载延迟,效率高
 */

// 静态内部类完成
class Singleton{
    // 构造器私有化
    private Singleton(){}

    // 静态内部类,该类中有一个静态属性 Singleton
    private static class SingletonInstance{
        private static final Singleton INSTANCE = new Singleton();
    }

    // 提供一个静态方法,直接返回SingletonInstance.INSTANCE
    public static Singleton getInstance(){
        return SingletonInstance.INSTANCE;
    }
}

推荐使用

package com.cedric.singleton.type07;

public class SingletonTest07 {
    public static void main(String[] args) {
        Singleton singleton = Singleton.INSTANCE;
        Singleton singleton1 = Singleton.INSTANCE;
        System.out.println(singleton == singleton1);

        System.out.println(singleton.hashCode());
        System.out.println(singleton1.hashCode());

        singleton.sayOK();
    }
}

// 使用枚举可以实现单例
enum Singleton{
    INSTANCE;
    public void sayOK(){
        System.out.println("OK");
    }
}

单例模式注意事项和细节说明

1.单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁
  的对象,使用单例模式可以提高系统性能
2.当想实例化一个单例类的时候,必须记住使用相应的获取对象的方法,而不是使用new
3.单例模式使用的场景:需要频繁的进行创建和销毁对象、创建对象时耗时过多或耗费资源过多,
但又经常用到的对象、工具类对象、频繁访问数据库或文件的对象(比如数据源、session工厂等)

标签:Singleton,singleton,模式,System,单例,println,设计模式,hashCode,out
来源: https://www.cnblogs.com/cedric1114/p/15380697.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有