Java Reference
In-Depth Information
MinCalculator minCalculator = (MinCalculator) arithmeticCalculator;
minCalculator.min(1, 2);
}
}
1-15. Introducing States to Your Beans
Problem
Sometimes you might want to add new states to a group of existing objects to keep track of their usage,
such as the calling count, the last modified date, and so on. It should not be a problem if all the objects
have the same base class. However, it's difficult for you to add such states to different classes if they are
not in the same class hierarchy.
Solution
You can introduce a new interface to your objects with an implementation class that holds the state
field. Then you can write another advice to change the state according to a particular condition.
How It Works
Suppose you want to keep track of the calling count of each calculator object. Because there is no field
for storing the counter value in the original calculator classes, you need to introduce one with Spring
AOP. First, let's create an interface for the operations of a counter.
package com.apress.springenterpriserecipes.calculator;
public interface Counter {
public void increase();
public int getCount();
}
Then just write a simple implementation class for this interface. This class has a count field for
storing the counter value.
package com.apress.springenterpriserecipes.calculator;
public class CounterImpl implements Counter {
private int count;
public void increase() {
count++;
}
public int getCount() {
return count;
}
}
 
Search WWH ::




Custom Search