设计模式十二————单例模式

单例模式定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点。
实现手段:将构造方法私有化,在类外不能创建对象。只能通过类内的静态方法创建对象。第一次访问这个静态方法时,创建一个对象,并将对象的引用返回;第二次访问这个对象时直接将引用返回。

code…


单例类:

public class Singleton {
    private static Singleton singleton=null;
    private Singleton(){}
    public static Singleton createObject()
    {
        if(singleton==null)
            singleton=new Singleton();
        return singleton;
    }
}

main方法:

public class Main {
    public static void main(String[] args) {
        Singleton single=Singleton.createObject();
        System.out.println(single);
        Singleton single1=Singleton.createObject();
        System.out.println(single1);
    }
}

运行结果:两个对象hashcode一样

com.Singleton.Singleton@74a14482
com.Singleton.Singleton@74a14482

先将单例类内的引用悬空,每次访问静态方法时先判断引用是否为空,如果为空则表明时第一次访问,创建一个对象让引用指向并返回;如果不为空,则表明不是第一次访问,直接将引用返回即可。
缺点:在多线程中,如果多个线程同时访问这个静态方法,如果引用为null的判断都成立,那么每个线程都会创建一个实例。如下:

public class Main {
    static class thread implements Runnable
    {
        @Override public void run() {
            Singleton singleton=Singleton.createObject();
            System.out.println(singleton);
        }
    }
    public static void main(String[] args) {
        Thread thread1=new Thread(new thread());
        Thread thread2=new Thread(new thread());
        Thread thread3=new Thread(new thread());
        Thread thread4=new Thread(new thread());
        Thread thread5=new Thread(new thread());
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }
}

由于程序运行较快,难以模拟多个线程同时启动,因此在if条件中使线程休眠1s:

 if(singleton==null)
 {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        singleton=new Singleton();
 }

运行结果出现五个不用的对象,单例模式被破坏。

com.Singleton.Singleton@60cb300b
com.Singleton.Singleton@dbefca
com.Singleton.Singleton@63158364
com.Singleton.Singleton@e4c242a
com.Singleton.Singleton@6fec3797

解决方法:

  • 对方法进行同步,用synchronized来修饰方法。
  • 在静态块中创建对象,并赋值给引用
第二种方法代码:
public class Singleton {
    private static Singleton singleton=null;
    static {
        singleton=new Singleton();
    }
    private Singleton(){}
    public static Singleton createObject()
    {
        return singleton;
    }
}

因为类只加载一次,对应对象创建一次。所以在静态块中创建对象。
破解方法:可以通过反射来破解。无论用什么方法写的单例模式,都可以通过反射来破解。
反射中最简单的破解方法,得到单例的Class对象,得到构造方法,然后实例化。

public class Main {
    public static void main(String[] args)throws Exception{
        Class single=Class.forName("com.Singleton.Singleton");
        Constructor constructor=single.getDeclaredConstructor();
        constructor.setAccessible(true);
        Singleton singleton1=(Singleton) constructor.newInstance();
        Singleton singleton2=(Singleton) constructor.newInstance();
        Singleton singleton3=(Singleton) constructor.newInstance();
        Singleton singleton4=(Singleton) constructor.newInstance();
        System.out.println(singleton1);
        System.out.println(singleton2);
        System.out.println(singleton3);
        System.out.println(singleton4);
    }
}

这样得到四个不同的对象。


上一篇
---The End---
下一篇
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值