jdk中的单例模式

1、java.lang.reflect.Proxy类

它的私有构造方法:

  private Proxy() {
    }
它的共有获取对象的方法:

public static Object newProxyInstance(ClassLoader loader,
       Class[] interfaces,
       InvocationHandler h)
 throws IllegalArgumentException
    {
 if (h == null) {
     throw new NullPointerException();
 }

 /*
  * Look up or generate the designated proxy class.
  */
 Class cl = getProxyClass(loader, interfaces);

 /*
  * Invoke its constructor with the designated invocation handler.
  */
 try {
     Constructor cons = cl.getConstructor(constructorParams);
     return (Object) cons.newInstance(new Object[] { h });
 } catch (NoSuchMethodException e) {
     throw new InternalError(e.toString());
 } catch (IllegalAccessException e) {
     throw new InternalError(e.toString());
 } catch (InstantiationException e) {
     throw new InternalError(e.toString());
 } catch (InvocationTargetException e) {
     throw new InternalError(e.toString());
 }
    }

 

2、java.lang.Runtime类

    private static Runtime currentRuntime = new Runtime();

    /**
     * Returns the runtime object associated with the current Java application.
     * Most of the methods of class Runtime are instance
     * methods and must be invoked with respect to the current runtime object.
     *
     * @return  the Runtime object associated with the current
     *          Java application.
     */
    public static Runtime getRuntime() {
 return currentRuntime;
    }

    /** Don't let anyone else instantiate this class */
    private Runtime() {}

你可能感兴趣的:(Singloton)