Comments on the Laddad article, Part 1:
Beware that the way Laddad uses the phrase "mix-in classes" is not the only way that phrase is used. (Also, the first word is more commonly spelled as "mixin".)
The section entitled "Weaving example" includes some manually woven
code, namely the CreditCardProcessorWithLogging
class.
The logging messages in that example are messed up. The messages in
the debit
method say that the credit
operation is starting or completing. Conversely, the first message
in the credit
method says that the debit
method is starting.
Moreover, the code in that example fails to include requirement number 3 from the list
immediately above it, "Log any exception thrown by each public
operation." To address this, each of the two methods
(debit
and credit
) would need an appropriate
try
/catch>
construct. For example, here is
a possible version of debit
:
public void debit(CreditCard card, Money amount) throws InvalidCardException, NotEnoughAmountException, CardExpiredException { _logger.log("Starting CreditCardProcessor.debit(CreditCard, Money) " + "Card: " + card + " Amount: " + amount); try{ // Debiting logic _logger.log("Completing CreditCardProcessor.debit(CreditCard, Money) " + "Card: " + card + " Amount: " + amount); }catch(Exceptiion e){ _logger.log("Exception " + e + " CreditCardProcessor.debit(CreditCard, Money) " + "Card: " + card + " Amount: " + amount); throw e; } }
Later, in the section "AspectJ: An AOP implementation for Java",
the same example is reprised as the aspect
LogCreditCardProcessorOperations
. This example could be
enhanced a bit to log also any public operations that don't have the
card and amount as arguments. The code also got a bit bashed
somewhere on its way to publication. Each
place where "thisjoin point
" appears, it should
really be "thisJoinPoint
".