java.beans.PropertyChangeSupport通過注冊PropertyChangeListener可以令屬性在運行或者設計時被修改后,可以自動地通知外部世界。

java.beans.VetoableChangeSupport通過注冊VetoableChangeListener有能力拒絕被設置為某個數值的屬性。如果fireVetoableChange方法拋出了一個PropertyVetoException例外,就表明監聽者已經拒絕了屬性的改變,屬性改變將不會生效。

例子很簡單,看看就會明白,不多寫解釋了,看例子吧!

 

一個Bean類

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;


public class SomeBean
{
    
protected PropertyChangeSupport propertySupport; //屬性改變通知支持
    protected VetoableChangeSupport vetoableChangeSupport; //反對屬性改變支持
    private String changableValue;


    
public SomeBean()
    
{
        propertySupport 
= new PropertyChangeSupport(this);//使本對象有屬性改變通知監聽器的能力
        vetoableChangeSupport = new VetoableChangeSupport(this);////使監聽器有反對本對象屬性改變的能力
        
//這里添加支持與象使用observer模式的實現接有口異曲同工之效
    }



    
public void setChangableValue(String newValue) throws PropertyVetoException
    
{
        String oldValue;
        
if((null == changableValue) || !changableValue.equals(newValue))
        
{
            oldValue 
= changableValue;
            changableValue 
= newValue;
            vetoableChangeSupport.fireVetoableChange(
"changableValue", oldValue, newValue);//這個在前
            propertySupport.firePropertyChange("changableValue", oldValue, newValue);//這個在后
            
//如果兩個監聽器的次序調過來,你想想會有什么問題?你期待的效果沒有達到哦~~~~~~~~~~~~~~~
        }

    }



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



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



    
public void addVetoableChangeListener(VetoableChangeListener listener)
    
{
        vetoableChangeSupport.addVetoableChangeListener(listener);
    }



    
public void removeVetoableChangeListener(VetoableChangeListener listener)
    
{
        vetoableChangeSupport.removeVetoableChangeListener(listener);

    }



    
public String getChangableValue()
    
{
        
return changableValue;
    }


}

 

一個監聽器(這里為了方便,把兩種監聽職能寫在一個類中)

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;


public class SomeBeanPropertyListener implements PropertyChangeListener,VetoableChangeListener
{
    
public void propertyChange(PropertyChangeEvent evt)
    
{
        System.out.println(
"\""+evt.getNewValue() +"\" is setted to replace the old value \""+evt.getOldValue()+"\"");
    }


    
public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException
    
{
        
if(evt.getNewValue().equals("invalidValue")){
            System.out.println(
"\""+evt.getNewValue() +"\" is try to replace the old value \""+evt.getOldValue()+"\"");
            
throw new PropertyVetoException("What you set \"invalidValue\"is a invalid value! operate fail!", evt);
        }

    }

}

 

測試一下

public class Test
{
    
public static void main(String[] args)
    
{
        SomeBean someBean 
= new SomeBean();
        someBean.addPropertyChangeListener(
new SomeBeanPropertyListener());
        someBean.addVetoableChangeListener(
new SomeBeanPropertyListener());
        
try
        
{
            someBean.setChangableValue(
"someValue");
            someBean.setChangableValue(
"anotherValue");
            someBean.setChangableValue(
"invalidValue");
        }

        
catch(PropertyVetoException e)
        
{
            System.err.println(e.getMessage());
        }

    }

}

 

結果輸出:

"someValue" is setted to replace the old value "null"
"anotherValue" is setted to replace the old value "someValue"
"invalidValue" is try to replace the old value "anotherValue"
What you set 
"invalidValue"is a invalid value! operate fail!