Let's say I have some variable defined using the statementint someVariable;
. While the code runs, the variable's value changes.
How can I track the changes in this variable? How could I implement some Listener that behaves like onSomeVariableChangedListener?
I also need to know when some other method in one page has been executed so I can set a Listener in another class.
In short, to use a simple ChangeListener one should follow these steps: Create a new ChangeListener instance. Override the stateChanged method to customize the handling of specific events. Use specific functions of components to get better undemanding of the event that occurred.
Right-click a variable or expression that you want to watch and choose Add Inline Watch in the context menu. An inline watch will be created for that variable or expression.
A change listener is similar to a property change listener. A change listener is registered on an object — typically a component, but it could be another object, like a model — and the listener is notified when the object has changed.
Variable ReassignmentThere's no rule in Java that says that once a variable is declared its values cannot be changed. As a result, your variable does not have to stay the same throughout your program. There is no need to state the data type of a variable when you reassign it.
Java gives you a simple Observer pattern implementation for this kind of thing, but you'll need to set your observed variable within a method that manages listener notifications. If you can't extend Observable, you can either use composition (i.e., have an Observable instance in your class to manage notifications), or you can take a look at java.util.Observable to get an idea of how to roll your own version.
Flux.java
import java.util.Observable; public class Flux extends Observable { private int someVariable = 0; public void setSomeVariable(int someVariable) { synchronized (this) { this.someVariable = someVariable; } setChanged(); notifyObservers(); } public synchronized int getSomeVariable() { return someVariable; } }
Heraclitus.java
import java.util.Observable; import java.util.Observer; public class Heraclitus implements Observer { public void observe(Observable o) { o.addObserver(this); } @Override public void update(Observable o, Object arg) { int someVariable = ((Flux) o).getSomeVariable(); System.out.println("All is flux! Some variable is now " + someVariable); } }
This is one of the many reasons to hide variables behind setter/getter pairs. Then, in the setter you can notify your listener that this variable has been modified in the appropriate way. As the others have commented, there is no built in way to do exactly what you want, you need to implement it yourself.
Alternatively Benjamin brings up an interesting pattern, called the Decorator pattern, which might be useful to you if the code in question cannot be modified. You can look up more info at Wikipedia
The idea is to build a compatible wrapper around an object. Lets say your object in question is of type MyClass.
class MyClass{ public void doFunc(){...} } class MyLoggedClass extends MyClass{ MyClass myObject; public void doFunc(){ //Log doFunc Call myObject.doFunc(); } }
instead of
MyClass object = new MyClass();
You would use
MyClass object = new MyLoggedClass(new MyClass());
Now your rest of the code would use object as per normal, except that each function call will be logged, or notified, etc.
As you will see in Wikipedia, this is typically done via an interface that the class in question inherits from, but this may not be possible in your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With