Introduction to AOP Exercises for Wednesday, 2003/09/24

advertisement
Introduction to AOP
Exercises for Wednesday, 2003/09/24
Exercise 5. (repeated and corrected from Tuesday) Write a class containing a method that computes binomial coefficients using the plain recursive
algorithm.
bc(n, 0) = 1
bc(n, n) = 1
bc(n, m) = bc(n − 1, m − 1) + bc(n − 1, m)
Write an aspect that counts the number of recursive calls and outputs
the result when the top-most call returns.
Write a caching aspect that stores all calculated values of bc and uses the
cached values, instead of re-evaluating them in the recursion.
Use both aspects to see how much you saved.
NEW: Write a version of the caching aspect that uses a new cache for
each top-level call of the method. This allows you to use a more efficient
cache data structure, that can hold exactly those recursive computations you
will need (look at the values you need in Pascal’s triangle!). Use percflow
association to make sure your aspect could be used concurrently by several
threads.
Exercise 6. Look at the access restrictions of exercise 4 again. Reimplement
them (if that is possible) using declare error.
Exercise 7. Explore the interaction between privileged aspects and private
member introduction.
Write a base class that declares a private field. Then add a privileged
access which introduces a private field of the same name to the same class.
Does the compiler accept this? Are the fields the same? Add a pointcut
to the aspect that selects access to the field (by giving its name). Which field
does the pointcut select? To which does a reference in advice refer?
Exercise 8. Using the process of member introduction, declaring default
implementations for interfaces, add an instantiation counter to each of a
bunch of classes, and let that be incremented each time an object of the class
gets created. Dump the results before exiting.
Download