2015年9月21日 星期一

Singleton Design Pattern 的範例

Singleton Design Pattern 的類別,只能實例化一次:
public class Husband {

  private static final Husband yourlover = new Husband();
  private static String name = "Peter" ;
  
  private Husband(){}

  public static Husband getYourLover(){
      System.out.println("Your Husband: " + name);
    return yourlover;
  }
} 

public class Demo {
    public static void main(String[] args){
        Husband.getYourLover();
    }
}