舉例以下:java
1 public static Boolean valueof(boolean a){ 2 return a?Boolean.TRUE:Boolean.FALSE; 3 }
舉例以下:設計模式
1 public interface Service{ 2 //service-specific methods go here 3 } 4 5 public interface Provider{ 6 Service newService(); 7 } 8 9 public class Services{ 10 private Services(){} 11 12 private static final Map<String, Provider> providers=new ConcurrentHashMap<String, Provider>(); 13 public static final String DEFAULT_PROVIDER_NAME="<def>"; 14 15 //provider registration API 16 public static void registerDefaultProvider(Provider p){ 17 registerProvider(DEFAULT_PROVIDER_NAME, p); 18 } 19 public static void registerProvider(String name,Provider p){ 20 providers.put(name, p); 21 } 22 23 //service access API 24 public static Service newInstance(){ 25 return newInsance(DEFAULT_PROVIDER_NAME); 26 } 27 public static Service newInsance(String name){ 28 Provider provider= providers.get(name); 29 if(provider==null){ 30 throw new IllegalArgumentException("No provider registered with name:"+ name); 31 } 32 return provider.newService(); 33 } 34 } 35
重疊構造器模式舉例:安全
1 public class NutritionFacts 2 { 3 private int servingSize; //required; 4 private int servings; //required; 5 private int calorier; //optioned; 6 private int fat; //optioned; 7 private int sodium; //optioned; 8 private int carbohydrate;//optioned; 9 10 public NutritionFacts(int servingSize,int servings){ 11 this(servingSize, servingSize, 0); 12 } 13 14 public NutritionFacts(int servingSize,int servings,int calorier){ 15 this(servingSize, servings, calorier,0); 16 } 17 18 public NutritionFacts(int servingSize,int servings,int calorier,int fat){ 19 this(servingSize, servings, calorier,fat,0,0); 20 } 21 22 public NutritionFacts(int servingSize,int servings,int calorier,int fat,int sodium){ 23 this(servingSize, servings, calorier,fat, sodium,0); 24 } 25 26 public NutritionFacts(int servingSize,int servings,int calorier,int fat,int sodium,int carbohydrate){ 27 this.servingSize=servingSize; 28 this.servings=servings; 29 this.calorier=calorier; 30 this.fat=fat; 31 this.sodium=sodium; 32 this.carbohydrate=carbohydrate; 33 } 34 } 35
javaBeans模式舉例:框架
1 public class NutritionFacts 2 { 3 private int servingSize; //required; 4 private int servings; //required; 5 private int calorier; //optioned; 6 private int fat; //optioned; 7 private int sodium; //optioned; 8 private int carbohydrate;//optioned; 9 10 public NutritionFacts(){} 11 12 public int getServingSize() 13 { 14 return servingSize; 15 } 16 17 public void setServingSize(int servingSize) 18 { 19 this.servingSize = servingSize; 20 } 21 22 public int getServings() 23 { 24 return servings; 25 } 26 27 public void setServings(int servings) 28 { 29 this.servings = servings; 30 } 31 32 public int getCalorier() 33 { 34 return calorier; 35 } 36 37 public void setCalorier(int calorier) 38 { 39 this.calorier = calorier; 40 } 41 42 public int getFat() 43 { 44 return fat; 45 } 46 47 public void setFat(int fat) 48 { 49 this.fat = fat; 50 } 51 52 public int getSodium() 53 { 54 return sodium; 55 } 56 57 public void setSodium(int sodium) 58 { 59 this.sodium = sodium; 60 } 61 62 public int getCarbohydrate() 63 { 64 return carbohydrate; 65 } 66 67 public void setCarbohydrate(int carbohydrate) 68 { 69 this.carbohydrate = carbohydrate; 70 } 71 72 73 } 74
Builder Pattern舉例:ide
1 public class NutritionFacts 2 { 3 private int servingSize; //required; 4 private int servings; //required; 5 private int calorier; //optioned; 6 private int fat; //optioned; 7 private int sodium; //optioned; 8 private int carbohydrate;//optioned; 9 10 public static class Builder{ 11 private int servingSize; 12 private int servings; 13 14 private int calorier=0; 15 private int fat=0; 16 private int carbohydrate=0; 17 private int sodium=0; 18 19 public Builder(int servingSize,int servings){ 20 this.servingSize=servingSize; 21 this.servings=servings; 22 } 23 24 public Builder calories(int val){ 25 calorier=val; 26 return this; 27 } 28 public Builder fat(int val){ 29 fat=val; 30 return this; 31 } 32 public Builder carhohydrate(int val){ 33 carbohydrate=val; 34 return this; 35 } 36 public Builder sodium(int val){ 37 sodium=val; 38 return this; 39 } 40 41 public NutritionFacts build(){ 42 return new NutritionFacts(this); 43 } 44 } 45 46 private NutritionFacts(Builder builder){ 47 servingSize=builder.servingSize; 48 servings=builder.servings; 49 calorier=builder.calorier; 50 fat=builder.fat; 51 sodium=builder.sodium; 52 carbohydrate=builder.carbohydrate; 53 } 54 55 public static void main(String[] args) 56 { 57 NutritionFacts coca=new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carhohydrate(27).build(); 58 59 System.out.println(coca.servingSize+";"+coca.servings+";"+coca.calorier+";"+coca.fat+";"+coca.sodium+";"+coca.carbohydrate); 60 } 61 } 62