单例模式之Java
package com.sc.test; public class SingletonMain { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Start"); Singleton obj1 = Singleton.getInstance(); Singleton obj2 = Singleton.getInstance(); if(obj1 == obj2){ System.out.println("obj1和obj2是同一个对象实例"); }else{ System.out.println("obj1和obj2并非同一对象实例"); } System.out.println("end"); } }
package com.sc.test; public class Singleton { private static Singleton singleton = new Singleton(); private Singleton(){ System.out.println("已产生实例对象"); } public static Singleton getInstance(){ return singleton; } }