Java Reference
In-Depth Information
public interface ReservationConstants
{
public static final int MINHEAD = 25;
public static final double MINTOTAL = 495.95;
}
A reservation for too small an audience or that generates too little income is invalid. To avoid
creating an instance of
Reservation
when a request is invalid, you might place business
logic checks and exception throwing in the constructor for
Reservation
or in an
init()
method that this constructor calls. But this logic is fairly independent of the normal function
of a
Reservation
object once it is created.
Introducing a builder will make the
Reservation
class simpler, leaving it with methods
that concentrate on behavior other than construction. Using a builder also creates
an opportunity to validate the parameters of a
Reservation
object with different reactions
to invalid parameters. Finally, by moving the construction job to a
ReservationBuilder
subclass, you allow construction to occur gradually, as the parser finds a reservation's
attributes. Figure 15.2 shows concrete
ReservationBuilder
subclasses that differ in how
forgiving they are of invalid parameters.
Figure 15.2. Builders can be forgiving or unforgiving as they try to create a valid object
from a given set of parameters.