Java Reference
In-Depth Information
validity of its state. A better approach is for PendingOrder to have an updateDeliv-
eryInfo() method that verifies the delivery information and updates PendingOrder .
PlaceOrderService can handle the fourth responsibility itself by instantiating
PlaceOrderServiceResult . PendingOrder just needs to return a status code indicat-
ing whether or not the delivery information was valid.
Now that we have figured out how these responsibilities are assigned, let's look
at the code for the updateDeliveryInfo() method. Listing 3.3 shows the Place-
OrderServiceImpl class and its updateDeliveryInfo() method.
Listing 3.3
PlaceOrderServiceImpl
public class PlaceOrderServiceImpl implements PlaceOrderService {
private PendingOrderRepository pendingOrderRepository;
public PlaceOrderService(PendingOrderRepository repository) {
this.pendingOrderRepository = repository;
}
Takes
PendingOrderRepository parameter B
public PlaceOrderServiceResult updateDeliveryInfo(
String pendingOrderId,
Address deliveryAddress,
Date deliveryTime) {
PendingOrder pendingOrder =
pendingOrderRepository
.findOrCreatePendingOrder(
C Gets PendingOrder from
PendingOrderRepository
pendingOrderId);
boolean success =
pendingOrder.updateDeliveryInfo(
deliveryAddress,
deliveryTime);
return new PlaceOrderServiceResult(success, pendingOrder);
}
}
D Invokes
PendingOrder
Creates
PlaceOrderServiceResult
E
Here's what this code does:
PlaceOrderServiceImpl is configured with PendingOrderRepository via construc-
tor injection. It has a constructor that takes PendingOrderRepository as a parame-
ter, and stores it in a field. Later in chapter 7 you will see how the Spring
framework is used to instantiate and configure PlaceOrderServiceImpl .
The updateDeliveryInfo() method calls PendingOrderRepository to get Pending-
Order .
B
C
 
Search WWH ::




Custom Search