-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRecipe.java
More file actions
31 lines (27 loc) · 910 Bytes
/
Copy pathRecipe.java
File metadata and controls
31 lines (27 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Missing imports, just for demonstration use.
public final class Recipe{
private String name;
private List<Ingredient> ingredients;
private List<Step> steps;
public Recipe(String name, Ingredient[] ingredients, Step[] steps){
this.name = name;
ingredients = new ArrayList<>();
for (Ingredient ingredient : ingredients) {
this.ingredients.add(ingredient);
}
steps = new ArrayList<>();
for (Step step : steps) {
this.steps.add(step);
}
}
public List<Ingredient> getIngredients(){
return new ArrayList<Ingredient>(ingredients);
// Defensive copy, assuming the Ingredient and Step classes are immutable designed.
}
public String getName(){
return name; // String is immutable, no need for defensive copy.
}
public List<Step> getSteps(){
return new ArrayList<Step>(steps); // Defensive copy, what if the Step Class is mutable?
}
}