Java/Kotlin implements monitoring of member variables

The general approach is as follows:
1. Create a class PropertyChangeAware, create a member variable changeSupport in it and initialize this member variable, it can record the changes of other class member variables
2. Inherit the class that needs to be monitored from PropertyChangeAware, so that this class is The changeSupport included
3. Write the set method of the member variable, and record the value change in changeSupoort.
3. Add a listener to the class that needs to be monitored, and actually add a listener to changeSupoort and implement the propertyChange method. So whenever the member variable calls the set method, changeSupoort will be recorded, and the propertyChange method will be executed

  • This is the PropertyChangeAware class
package edu.wang.af;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class PropertyChangeAware  {
    
    
    protected PropertyChangeSupport changeSupport=new PropertyChangeSupport(this);

   

    public void addPropertyChangeListener(PropertyChangeListener listener) {
    
    
        changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
    
    
        changeSupport.removePropertyChangeListener(listener);
    }
}

  • This is the Perosn class, test the effect of monitoring
package edu.wang.af;

public class Person extends PropertyChangeAware{
    
    
    private String name;
    private int age;
    Person(String name,int age){
    
    
        super();
        this.name=name;
        this.age=age;
    }

    public String getName() {
    
    
        return name;
    }

    public void setName(String name) {
    
    
        String oldName=this.name;
        this.name=name;
        changeSupport.firePropertyChange("name",oldName,name);
    }

    public static void main(String[] args) {
    
    
        Person person=new Person("何老表",69);
        person.addPropertyChangeListener(e->{
    
    
            System.out.println("属性名:"+e.getPropertyName()+"  旧值:"+e.getOldValue()+"  新值:"+e.getNewValue());
        });
        person.setName("梁胖");
    }
}

  • The results of
    Insert picture description here
    course, if you use Kotlin delegate attribute, the set and get methods to delegate to other categories, will be more simple

For example, I want to monitor the balance change of an account

  • Below is the PropertyChangeAware class
package com.wang.bank

import java.beans.PropertyChangeListener
import java.beans.PropertyChangeSupport

open class PropertyChangeAware {
    
    
    protected val changeSupport= PropertyChangeSupport(this)
    fun addPropertyChangeListener(listener: PropertyChangeListener)
    =changeSupport.addPropertyChangeListener(listener)
    fun removePropertyChangeListener(listener: PropertyChangeListener)=
        changeSupport.removePropertyChangeListener(listener)
}
  • Below is ObservableProperty
package com.wang.bank

import java.beans.PropertyChangeSupport
import kotlin.reflect.KProperty

class ObservableProperty(var propValue:Double, private val changeSupport: PropertyChangeSupport) {
    
    
    operator fun getValue(account: Account,prop:KProperty<*>)=propValue
    operator fun setValue(account: Account,prop: KProperty<*>,newValue: Double) {
    
    

        val oldValue = propValue
        propValue = newValue
        changeSupport.firePropertyChange(prop.name,oldValue,newValue)
    }
}
package com.wang.bank

class Account(val name:String, balance:Double):PropertyChangeAware() {
    
    
    var balance:Double by ObservableProperty(balance,changeSupport)

}

The above code delegates the properties to ObservableProperty, which is very concise
and there is a simpler method: use Delegates.observable()

package com.wang.bank2

import com.wang.bank.PropertyChangeAware
import kotlin.properties.Delegates
import kotlin.reflect.KProperty

class Account2(val name:String,balance:Double) :PropertyChangeAware(){
    
    

    private val observer={
    
    
        //顺序不可调换
            prop:KProperty<*>,oldValue:Double,newValue:Double->
        changeSupport.firePropertyChange(prop.name,oldValue, newValue)
    }
    var balance:Double by Delegates.observable(balance,observer)
}

It can be seen that Kotlin's advantages are indeed more concise than Java

Guess you like

Origin blog.csdn.net/m0_47202518/article/details/110392311