Pārlūkot izejas kodu

Added Builder as inner static class for creating Voyage aggregates sing the builder pattern.

peter_backlund 18 gadus atpakaļ
vecāks
revīzija
ef2f73744f

+ 36
- 0
dddsample/src/main/java/se/citerus/dddsample/domain/model/carrier/Voyage.java Parādīt failu

@@ -1,6 +1,12 @@
1 1
 package se.citerus.dddsample.domain.model.carrier;
2 2
 
3
+import org.apache.commons.lang.Validate;
3 4
 import se.citerus.dddsample.domain.model.Entity;
5
+import se.citerus.dddsample.domain.model.location.Location;
6
+
7
+import java.util.ArrayList;
8
+import java.util.Date;
9
+import java.util.List;
4 10
 
5 11
 /**
6 12
  * A Voyage.
@@ -62,4 +68,34 @@ public class Voyage implements Entity<Voyage> {
62 68
   // Needed by Hibernate
63 69
   private Long id;
64 70
 
71
+  /**
72
+   * Builder pattern is used for incremental construction
73
+   * of a Voyage aggregate. This serves as an aggregate factory. 
74
+   */
75
+  public static final class Builder {
76
+
77
+    private final List<CarrierMovement> carrierMovements = new ArrayList<CarrierMovement>();
78
+    private final VoyageNumber voyageNumber;
79
+    private Location departureLocation;
80
+
81
+    public Builder(VoyageNumber voyageNumber, Location initialDepartureLocation) {
82
+      Validate.notNull(voyageNumber, "Voyage number is required");
83
+      Validate.notNull(voyageNumber, "Departure location is required");
84
+
85
+      this.voyageNumber = voyageNumber;
86
+      this.departureLocation = initialDepartureLocation;
87
+    }
88
+
89
+    public Builder addMovement(Location arrivalLocation, Date departureTime, Date arrivalTime) {
90
+      carrierMovements.add(new CarrierMovement(departureLocation, arrivalLocation, departureTime, arrivalTime));
91
+      this.departureLocation = arrivalLocation;
92
+      return this;
93
+    }
94
+
95
+    public Voyage build() {
96
+      return new Voyage(voyageNumber, new Schedule(carrierMovements));
97
+    }
98
+
99
+  }
100
+
65 101
 }