java设计模式-Builder

时间:2022-08-05 20:19:49

本文主要用来介绍java设计模式之一的Builder设计模式,以及和常用描述一个对象方法的比较
- 重叠构造器
- JavaBean
- Builder设计模式

三种方法分别用一个类,来描述包装食品外面显示的营养成分标签

重叠构造器

/**
* 重叠构造器
* @author Administrator
*
*/

public class NutritionFacts {
private final int servingSize; // (mL) required
private final int servings; // (per container) required
private final int calories; // optional
private final int fat; // (g) optional
private final int sodium; // (mg) optional
private final int carbohydrate; // (g) optional

public NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
}

public NutritionFacts(int servingSize, int servings, int calories) {
this(servingSize, servings, calories, 0);
}

public NutritionFacts(int servingSize, int servings, int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}

public NutritionFacts(int servingSize, int servings, int calories, int fat,
int sodium) {
this(servingSize, servings, calories, fat, sodium, 0);
}

public NutritionFacts(int servingSize, int servings, int calories, int fat,
int sodium, int carbohydrate) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = sodium;
this.carbohydrate = carbohydrate;
}

public static void main(String[] args) {
NutritionFacts cocaCola = new NutritionFacts(240, 8, 100, 0, 35, 27);
}
}

采用重叠构造器描述一个对象,适合参成员变量比较少的类

缺点比较明显:
1. 不得不传递不必要的参数,上例中,每个参数都需要传递,最短的需要传递两个参数,最长的需要传递6个参数
2. 难以阅读,参数太长,容易传递错误的参数

JavaBean

/**
* JavaBena描述一个对象
* @author Administrator
*
*/

public class NutritionFacts {
//成员变量
private int servingSize = -1;
private int servings = -1;
private int calories = 0;
private int fat = 0;
private int sodium = 0;
private int carbohydrate = 0;

public NutritionFacts() { }

// Setters
public void setServingSize(int val) { servingSize = val; }
public void setServings(int val) { servings = val; }
public void setCalories(int val) { calories = val; }
public void setFat(int val) { fat = val; }
public void setSodium(int val) { sodium = val; }
public void setCarbohydrate(int val) { carbohydrate = val; }


public static void main(String[] args) {
NutritionFacts cocaCola = new NutritionFacts();
cocaCola.setServingSize(240);
cocaCola.setServings(8);
cocaCola.setCalories(100);
cocaCola.setSodium(35);
cocaCola.setCarbohydrate(27);
}
}

以上是javaBean来描述一个对象,也是我们比较常用的方式,通过Setter、Getter来传递数据

优点:
1. 弥补了重叠构造方法的不足,没有带有较长参数的函数,参数分别设置
2. 代码阅读方便
缺点:
1. 线程不安全,构造的过程分到了几个调用,也就是Setter来传递参数,可能导致错误

Builder模式

/**
* JavaBena描述一个对象
* @author Administrator
*
*/

public class NutritionFacts {
//成员变量
private int servingSize = -1;
private int servings = -1;
private int calories = 0;
private int fat = 0;
private int sodium = 0;
private int carbohydrate = 0;

public NutritionFacts() { }

// Setters
public void setServingSize(int val) { servingSize = val; }
public void setServings(int val) { servings = val; }
public void setCalories(int val) { calories = val; }
public void setFat(int val) { fat = val; }
public void setSodium(int val) { sodium = val; }
public void setCarbohydrate(int val) { carbohydrate = val; }


public static void main(String[] args) {
NutritionFacts cocaCola = new NutritionFacts();
cocaCola.setServingSize(240);
cocaCola.setServings(8);
cocaCola.setCalories(100);
cocaCola.setSodium(35);
cocaCola.setCarbohydrate(27);
}

public static void main(String[] args) {
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).
calories(100).sodium(35).carbohydrate(27).build();
}
}

先通过设置内部Builder设置每个参数,然后将赋值参数的自身传递出去

return this

之后通过NutritionFacts(Builder builder)构造方法,将赋值后的Builder参数拿出来,赋值给自己的成员变量

优点:
1. 可以检查参数传递的类型是否一致
2. 随意传递任意个参数
缺点:
1. 比较冗长,需要创建构造器


总结

如果类的构造器有多个参数时,使用Builder设计模式是不错的方式。