A Production-Time Aspect
For my final aspect, I want to describe a simple order-management application. The program allows stock items to be added to an order. The order is then completed and billed. It’s a really simple commerce application. Listing 7 shows the output from the program, in which an order is created to which two items are added. Once the order is complete, it’s billed for invoicing.
Listing 7 A Really, Really Simple Commerce Application!
Created an instance of StockItem Created another instance of StockItem Now completing the order for reporting Now in the after code Calculated order value 670 Order is complete, ready for reporting Retrieved order value 670 Order has a value of 670
I won’t go into too many details on this code. I wrote it so it’s ultra simple; you can view the files in the source.zip. It consists of three classes and a single aspect made up of the following:
- StockItem.java
- Order.java
- OrderManagement.java
- Charging.java (the aspect code)
The program flow is contained in OrderManagement.java (see Listing 8).
Listing 8 The Program Flow
public class OrderManagement { public static void main(String[] args){ StockItem item1 = new StockItem("A Toaster", "Electrical Goods", 450); StockItem item2 = new StockItem("A Kettle", "Electrical Goods", 220); Order myOrder = new Order("Satisfied Customer", new Date()); myOrder.addToOrder(item1); myOrder.addToOrder(item2); myOrder.completeOrder(); report(myOrder);
The program starts by instantiating two StockItem objects—a toaster and a kettle. Next, an order is instantiated to which I add the toaster and kettle objects. I then signal that the order is complete and call the report() method to display the results.