Java Reference
In-Depth Information
Builder
This code example shows how to use the Builder pattern to create an appointment for the PIM. The following list
summarizes each class's purpose:
AppointmentBuilder, MeetingBuilder - Builder classes
Scheduler - Director class
Appointment - Product
Address, Contact - Support classes, used to hold information relevant to the Appointment
InformationRequiredException - An Exception class produced when more data is required
For the base pattern, the AppointmentBuilder manages the creation of a complex product, which is an
Appointment in this example. The AppointmentBuilder uses a series of build methods— buildAppointment ,
buildLocation , buildDates , and buildAttendees — to create an Appointment and populate it.
Example A.11 AppointmentBuilder.java
1. import java.util.Date;
2. import java.util.ArrayList;
3.
4. public class AppointmentBuilder{
5.
6. public static final int START_DATE_REQUIRED = 1;
7. public static final int END_DATE_REQUIRED = 2;
8. public static final int DESCRIPTION_REQUIRED = 4;
9. public static final int ATTENDEE_REQUIRED = 8;
10. public static final int LOCATION_REQUIRED = 16;
11.
12. protected Appointment appointment;
13.
14. protected int requiredElements;
15.
16. public void buildAppointment(){
17. appointment = new Appointment();
18. }
19.
20. public void buildDates(Date startDate, Date endDate){
21. Date currentDate = new Date();
22. if ((startDate != null) && (startDate.after(currentDate))){
23. appointment.setStartDate(startDate);
24. }
25. if ((endDate != null) && (endDate.after(startDate))){
26. appointment.setEndDate(endDate);
27. }
28. }
29.
30. public void buildDescription(String newDescription){
31. appointment.setDescription(newDescription);
32. }
33.
34. public void buildAttendees(ArrayList attendees){
35. if ((attendees != null) && (!attendees.isEmpty())){
36. appointment.setAttendees(attendees);
37. }
38. }
39.
40. public void buildLocation(Location newLocation){
41. if (newLocation != null){
42. appointment.setLocation(newLocation);
43. }
44. }
45.
46. public Appointment getAppointment() throws InformationRequiredException{
47. requiredElements = 0;
48.
49. if (appointment.getStartDate() == null){
50. requiredElements += START_DATE_REQUIRED;
51. }
52.
 
Search WWH ::




Custom Search