Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create change listener for variable?

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.

like image 659
Sinigami Avatar asked Mar 15 '13 13:03

Sinigami


People also ask

How do you use listener change?

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.

How do you watch a variable in Java?

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.

What is change listener?

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.

Can I reassign a variable in Java?

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.


2 Answers

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);   } } 
like image 98
Kevin Condon Avatar answered Sep 17 '22 17:09

Kevin Condon


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.

like image 44
Karthik T Avatar answered Sep 17 '22 17:09

Karthik T