Home > Articles > Software Development & Management > Object Technology

  • Print
  • + Share This
Like this article? We recommend

So Where’s the Aspect?

The file Charging.java contains the aspect, which we’ll now example. Listing 9 shows an excerpt from the aspect.

Listing 9 The Charging (or Billing) Aspect

public aspect Charging {

    pointcut startCharging(Order order): target(order) &&
    call(void Order.completeOrder());

    after(Order order): startCharging(order) {
        System.out.println("Now in the after code");
        calculateTotalCharge(order);
    }

The first thing of importance is Listing 9 is the pointcut called startCharging(). This is initiated when a call is made to Order.completeOrder(). Remember completeOrder() from the next-to-last line of Listing 8? Well, once completeOrder() is called, our aspect starts.

What happens next? In Listing 9, the "after" code executes, at which point we see a call to println("Now in the after code"), followed by a call to a method called calculateTotalCharge(). The latter uses an enumeration to sum up the items contained in the order. Listing 10 shows the implementation of calculateTotalCharge() with the remainder of the aspect code.

Listing 10 The Remainder of the Aspect

public long Order.totalCharge = 0;
public long calculateTotalCharge(Order order) {
      Enumeration e = order.orderItems.elements();
      while (e.hasMoreElements()) {
          StockItem aStockItem = (StockItem)e.nextElement();
          order.totalCharge += aStockItem.itemFinancialValue;
      }

      System.out.println("Calculated order value " + order.totalCharge);
      return order.totalCharge;
  }

public long getTotalCharge(Order order) {
      System.out.println("Retrieved order value " + order.totalCharge);
      return order.totalCharge;
  }

Listing 10 includes a new data member called Order.totalCharge and two methods called calculateTotalCharge() and getTotalCharge(). The first method is used to calculate the total value of the items in the order; the second method allows for this summed value to be retrieved. Looking back at Listing 7, we see that the value of the order is 670. We can also see the way in which the aspect executes.

  • + Share This
  • 🔖 Save To Your Account