Java Reference
In-Depth Information
16. public Location getLocation(){ return location; }
17.
18. public void setDescription(String newDescription){ description = newDescription; }
19. public void setLocation(Location newLocation){ location = newLocation; }
20. public void setStartDate(Date newStartDate){ startDate = newStartDate; }
21. public void setEndDate(Date newEndDate){ endDate = newEndDate; }
22. public void setAttendees(ArrayList newAttendees){
23. if (newAttendees != null){
24. attendees = newAttendees;
25. }
26. }
27.
28. public void addAttendee(Contact attendee){
29. if (!attendees.contains(attendee)){
30. attendees.add(attendee);
31. }
32. }
33.
34. public void removeAttendee(Contact attendee){
35. attendees.remove(attendee);
36. }
37.
38. public String toString(){
39. return " Description: " + description + EOL_STRING +
40. " Start Date: " + startDate + EOL_STRING +
41. " End Date: " + endDate + EOL_STRING +
42. " Location: " + location + EOL_STRING +
43. " Attendees: " + attendees;
44. }
45. }
The
Scheduler
class makes calls to the
AppointmentBuilder
, managing the creation process through the
method
createAppointment
.
Example 1.12
Scheduler.java
1. import java.util.Date;
2. import java.util.ArrayList;
3. public class Scheduler{
4. public Appointment createAppointment(AppointmentBuilder builder,
5. Date startDate, Date endDate, String description,
6. Location location, ArrayList attendees) throws InformationRequiredException{
7. if (builder == null){
8. builder = new AppointmentBuilder();
9. }
10. builder.buildAppointment();
11. builder.buildDates(startDate, endDate);
12. builder.buildDescription(description);
13. builder.buildAttendees(attendees);
14. builder.buildLocation(location);
15. return builder.getAppointment();
16. }
17. }
The responsibilities of each class are summarized here:
Scheduler -
Calls the appropriate build methods on
AppointmentBuilder
; returns a complete
Appointment
object to its caller.
AppointmentBuilder -
Contains build methods and enforces business rules; creates the actual
Appointment
object.
Appointment -
Holds information about an appointment.
The
MeetingBuilder
class in
Example 1.13
demonstrates one of the benefits of the Builder pattern. To add
additional rules for the
Appointment
, extend the existing builder. In this case, the
MeetingBuilder
enforces an
additional constraint: for a meeting
Appointment
, start and end dates must be specified.
Example 1.13
MeetingBuilder.java
1. import java.util.Date;
2. import java.util.Vector;
3.
4. public class MeetingBuilder extends AppointmentBuilder{