Java Reference
In-Depth Information
Building a Counteroffer
The UnforgivingBuilder class will reject requests that are anything less than fully
formed. Suppose that the business decides that the software should make reasonable changes
to requests that are missing certain details about the reservation. Specifically, suppose that
an analyst asks you to set the headcount for an event to the minimum if this attribute is
missing. Similarly, if the dollars/head value is missing, you are to set it to be high enough so
that the total take is above the minimum. If you make either of these changes, you should
mark the reservation as a counteroffer that the reservation system will confirm with
the customer.
To meet this request, suppose that you add a Boolean counteroffer argument method to
the Reservation constructor and develop a new ForgivingBuilder class. As you build
a reservation in ForgivingBuilder , do the following.
1. If the reservation request specifies no headcount and no dollars/head, set
the headcount to the minimum and set dollars/head to the minimum total divided by
the headcount.
2. If there is no headcount but there is a dollars/head value, set the headcount to be at
least the minimum attendance and at least enough to generate enough money for
the event.
3. If there is a headcount but no dollars/head value, set the dollars/head value to be high
enough to generate the minimum take.
As before, your code should throw an exception if the reservation fails to specify a city or
a date, as there is no way to guess these values.
CHALLENGE 15.3
Write the code for the build() method of the ForgivingBuilder class.
The classes ForgivingBuilder and UnforgivingBuilder let you guarantee that
Reservation objects are always valid. Your design also gives you flexibility about what
action to take when there is a problem in constructing a reservation.
Summary
The B UILDER pattern separates the construction of a complex object from its representation.
This has the immediate effect of making a complex target class simpler. It lets a builder class
focus on the proper construction of an object, leaving the target class to focus on the operation
of a valid instance. This is especially useful when you want to ensure the validity of an object
before instantiating it and don't want the associated logic to appear in the target class's
constructors. A builder also accommodates step-by-step construction, which occurs when you
create an object by parsing text and may occur when you gather an object's parameters from
a user interface.
Search WWH ::




Custom Search