Java Reference
In-Depth Information
The test case calls
PendingOrder.updateDeliveryInfo()
with valid delivery infor-
mation and verifies that it updates
PendingOrder
and returns
true
.
Implementing the method
Because
PendingOrder
already defines a stub method, this test compiles without a
problem. But in order for it to pass, we need to replace the stub with a real imple-
mentation that validates the delivery information and updates
PendingOrder
.
PendingOrder
first checks that the delivery information is at least one hour in
the future by using the Java
Calendar
class. It then queries the database to validate
the delivery information.
The simplest approach is to encapsulate this query within
Res-
taurantRepository
and
define an
isRestaurantAvailable()
method. The
update-
DeliveryInfo()
method calls
isRestaurantAvailable()
and stores the delivery
information if it returns
true
:
public class PendingOrder {
private Date deliveryTime;
private Address deliveryAddress;
public boolean updateDeliveryInfo(
Address deliveryAddress,
Date deliveryTime) {
Calendar earliestDeliveryTime = Calendar.getInstance();
earliestDeliveryTime.add(Calendar.HOUR, 1);
if (deliveryTime.before(earliestDeliveryTime.getTime()))
bb
return false;
b
bbbbbbb
//How to access this?
bbbbbbb
RestaurantRepository restaurantRepository = …;
if (restaurantRepository
.isRestaurantAvailable(deliveryAddress, deliveryTime)) {
this.deliveryAddress = deliveryAddress;
this.deliveryTime = deliveryTime;
return true;
} else
return false;
}
}
One important design issue, which we haven't resolved, is how
PendingOrder
accesses
RestaurantRepository
. Let's look at how to do this.
Options for accessing a repository
Repositories are mainly used by the domain services, but they are also invoked by
some entities such as
PendingOrder
. To invoke a method on a repository object,
Search WWH ::
Custom Search