Java Reference
In-Depth Information
single point of creational control for its product, which other objects within the system can use. Like other
creational patterns, this makes things easier for clients in the software system, since they need only access the
Builder object to produce a resource.
The main drawback of this pattern is that there is tight coupling among the Builder, its product, and any other
creational delegates used during object construction. Changes that occur for the product created by the Builder
often result in modifications for both the Builder and its delegates.
Pattern Variants
At the most fundamental level, it is possible to implement a bare-bones Builder pattern around a single Builder
class with a creational method and its product. For greater flexibility, designers often extend this base pattern with
one or more of the following approaches:
Create an abstract Builder . By defining an abstract class or interface that specifies the creational methods, you
can produce a more generic system that can potentially host many different kinds of builders.
Define multiple create methods for the Builder . Some Builders define multiple methods (essentially, they
overload their creational method) to provide a variety of ways to initialize the constructed resource.
Develop creational delegates. With this variant, a Director object holds the overall Product create method and
calls a series of more granular create methods on the Builder object. In this case, the Director acts as the
manager for the Builder's creation process.
Related Patterns
Related patterns include Composite ( page 157). The Builder pattern is often used to produce Composite objects,
since they have a very complex structure.
Example
Note:
For a full working example of this code example, with additional supporting classes and/or a RunPattern class,
see “ Builder ” on page 343 of the “ Full Code Examples ” appendix.
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, an Appointment here.
The AppointmentBuilder uses a series of build methods—buildAppointment, buildLocation, buildDates, and
buildAttendees—to create an Appointment and populate it with data.
Example 1.10 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;
Search WWH ::




Custom Search