Java Reference
In-Depth Information
3.3 Implementing a domain model: an example
In this section you will see an example of how the techniques described in the pre-
vious section can be used to develop a domain model. I show how to do this by
developing the methods that are called to handle the
enter delivery information
request. I first implement the
PlaceOrderService
's
updateDeliveryInfo()
method we identified earlier. After that, I show how to implement a
PendingOrder
method, which is called by
updateDeliveryInfo()
. The end result is working and
tested
PlaceOrderService
and
PendingOrder
methods that verify the delivery
information and update
PendingOrder
. The required repository methods are also
identified. By studying this small example, you will learn an effective way to
develop and test a domain model. Since the repository methods call the persis-
tence framework, we won't implement those until chapters 4-6.
3.3.1
Implementing a domain service method
PlaceOrderService
, which is a domain model service, has an
updateDelivery-
Info()
method that is invoked when the user enters the delivery information.
This method has the following signature:
public interface PlaceOrderService {
PlaceOrderServiceResult updateDeliveryInfo(String pendingOrderId,
bbb
Address deliveryAddress,
bbb
Date deliveryTime);
}
Its parameters consist of
pendingOrderId
, which is the primary key of
Pending-
Order
in the database, and the
deliveryAddress
and
deliveryTime
parameters,
which specify the delivery information. It returns a
PlaceOrderServiceResult
,
which consists of a status code and
PendingOrder
.
When
updateDeliveryInfo()
is invoked,
PlaceOrderService
retrieves
Pending-
Order
or creates one if it does not exist. It then does one of the following:
If the delivery time is in the future and the delivery information is served by
at least one restaurant,
PlaceOrderService
updates
PendingOrder
with the
new delivery information. It returns a
PlaceOrderServiceResult
containing
a successful status code and
PendingOrder
.
■
If the delivery address and time are not served by any restaurant or the
delivery time is not in the future,
PlaceOrderService
leaves
PendingOrder
unchanged. It returns a
PlaceOrderServiceResult
containing a status code
indicating failure and
PendingOrder
.
■
Search WWH ::
Custom Search