Java Reference
In-Depth Information
change many methods to take repositories as parameters in order to pass them
from the services, which obtain them via constructor injection, to the methods
that use them.
In this example, passing
RestaurantRepository
to
PendingOrder
requires only
minor changes. We just need to change
PlaceOrderService
(in listing 3.3) to pass
RestaurantRepository
as an argument to
PendingOrder.updateDeliveryInfo()
,
which in turn requires
PlaceOrderService
's constructor to take it as a parameter.
Listing 3.6 shows the
PendingOrder.updateDeliveryInfo()
method.
Listing 3.6
PendingOrder
public class PendingOrder {
private Date deliveryTime;
private Address deliveryAddress;
public boolean updateDeliveryInfo(
RestaurantRepository restaurantRepository,
Address deliveryAddress,
Date deliveryTime) {
Calendar earliestDeliveryTime = Calendar.getInstance();
earliestDeliveryTime.add(Calendar.HOUR, 1);
if (deliveryTime.before(earliestDeliveryTime.getTime()))
bbbbbb
b
bb
return false;
if (restaurantRepository
.isRestaurantAvailable(deliveryAddress, deliveryTime)) {
this.deliveryAddress = deliveryAddress;
this.deliveryTime = deliveryTime;
return true;
} else
return false;
}
}
This method calls the
RestaurantRepository.isRestaurantAvailable()
method
and, if it succeeds, updates
PendingOrder
with the delivery information.
In order to get this class to compile, we need to define the
isRestaurantAvail-
able()
method:
public interface RestaurantRepository {
boolean isRestaurantAvailable(Address deliveryAddress,
Date deliveryTime)
…
}
Search WWH ::
Custom Search