java-对象的创建
一、考虑用静态工厂方法来替代构造器
- 静态工厂与设计模式中的工厂方法模式不同
- 与构造器比第一大优势:他们有具体的名称,而不是类名
- 与构造器比第二大优势:不必在每次调用它的时候都创建一个新对象
- 与构造器比第三大优势:它可以返回原返回类型的任何子类型的对象
- 与构造器比第四大优势:在创建参数化实例时,可以使代码变得更加简洁
举例如下:
1 public static Boolean valueof(boolean a){ 2 return a?Boolean.TRUE:Boolean.FALSE; 3 }
二、服务提供者框架
- 服务接口:提供者实例
- 提供者注册API:系统用来注册实现,让客服端访问它们
- 服务访问API:客服端用来获取服务的实例,是一个灵活的静态工厂
举例如下:
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
三、Builder 模式
- 当遇到多个构造器参数时考虑使用构建器
- 与重叠构造器模式、JavaBean模式相比,既能保证像重叠构造器模式那样安全,又能保证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(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举例:
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
作者:奋斗的珞珞
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.