如何打破单身人士?

时间:2013-12-13 05:35:01

标签: java

如何在java中打破单例类? 单例模式确保一个类只有一个实例,并提供对它的全局访问点。

该类的默认构造函数是私有的,这可以防止其他类直接实例化对象。

静态修饰符应用于返回对象的实例方法,然后使此方法成为可以在不创建对象的情况下访问的类级方法

修改

public class Singleton {
    private static Singleton singleInstance;

    private Singleton() {
        System.out.println("Singleton Constructor Running...");
    }
    public static Singleton getInstance() { 
        if (singleInstance == null) { 
            synchronized (Singleton.class) { 
            if (singleInstance == null) { 
                    singleInstance = new Singleton();
                }
            }
        } 
        return singleInstance; 
    }
}

使用反射打破单身人士

import java.lang.reflect.Constructor;

public class BreakSingletonUsingReflection {
    public static void main(String[] args) {
        Singleton instanceOne = Singleton.getInstance();
        Singleton instanceTwo = null;
        try {
            Constructor[] constructors = Singleton.class.getDeclaredConstructors();
            for (Constructor constructor : constructors) {
                //Below code will destroy the singleton pattern
                constructor.setAccessible(true);
                instanceTwo = (Singleton) constructor.newInstance();
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(instanceOne.hashCode());
        System.out.println(instanceTwo.hashCode());
    }
}

3 个答案:

答案 0 :(得分:2)

<强> 1

import java.lang.reflect.Constructor;


    public class ReflectionSingletonTest {

        public static void main(String[] args) {
            EagerInitializedSingleton instanceOne = EagerInitializedSingleton.getInstance();
            EagerInitializedSingleton instanceTwo = null;
            try {
                Constructor[] constructors = EagerInitializedSingleton.class.getDeclaredConstructors();
                for (Constructor constructor : constructors) {
                    //Below code will destroy the singleton pattern
                    constructor.setAccessible(true);
                    instanceTwo = (EagerInitializedSingleton) constructor.newInstance();
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(instanceOne.hashCode());
            System.out.println(instanceTwo.hashCode());
        }

    }

<强> 2

Singleton类:

package com.singleton.securitymgr;

public class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    private Singleton() {
        System.out.println("Singleton Constructor Running...");
    }

    public static final Singleton getInstance() {
        return INSTANCE;
    }
}

测试类:

package com.singleton.securitymgr;

import java.lang.reflect.Constructor;

public class Test {

    public static void main(String[] args) throws Exception {

        Singleton s = Singleton.getInstance();

        Class clazz = Singleton.class;

        Constructor cons = clazz.getDeclaredConstructor();
        cons.setAccessible(true);

        Singleton s2 = (Singleton) cons.newInstance();
    }
}

答案 1 :(得分:0)

单例只能用于每个classLoader。要打破它,请使用多个classLoaders

答案 2 :(得分:0)

另一种方法是将对象序列化存储在一个文件中并对其进行反序列化以获得一个新的对象。