JDK中的设计模式——单例模式

来源:互联网 发布:日语翻译哪个软件好 编辑:程序博客网 时间:2024/04/29 00:45

Src Code

相关类

Runtime

Runtime

Runtime runtime = Runtime.getRuntime();

实现方式

public class Runtime {    private static Runtime currentRuntime = new Runtime();    /**     * Returns the runtime object associated with the current Java application.     */    public static Runtime getRuntime() {        return currentRuntime;    }    private Runtime() {}    ......}

反射对单例的破坏

        Class<Runtime> runtimeClass = Runtime.class;        Constructor<Runtime> runtimeConstructor = runtimeClass.getDeclaredConstructor();        runtimeConstructor.setAccessible(true);        System.out.println(Runtime.getRuntime() == runtimeConstructor.newInstance());
0 0
原创粉丝点击