Design Patterns Java™ Workbook

(Michael S) #1
Chapter 15. Builder

Figure 15.1. A builder class offloads construction logic from a domain class and can
accept initialization parameters gradually, as a parser discovers them.

The classes that Figure 15.1 shows appear in the com.oozinoz.reservation package.
The ReservationBuilder class is abstract, as is its build() method. We will create
concrete subclasses of ReservationBuilder shortly. First, though, note that
the constructor for Reservation is protected.


CHALLENGE 15.1


The Reservation constructor has protected visibility. Explain either why this is
ideal or why it would be better to give this constructor private, public, or package
protection.

Building under Constraints.......................................................................................................................


You want to ensure that invalid Reservation objects are never instantiated. Specifically,
suppose that every reservation must have a non-null date and city. Further, suppose that
a business rule states that Oozinoz will not perform for fewer than 25 people or for less than
$495.95. To support this rule, you can create a ReservationConstants interface:

Free download pdf