美文网首页
constrained属性修改

constrained属性修改

作者: Mooner_guo | 来源:发表于2016-06-11 22:41 被阅读20次

有时候需要关注javabean的某些field的修改所关联的一些其他的东西(我个人认为是一些属性的限制)即(1)修改后做通知(2)是否可以拒绝修改。可以在setter里面做限制,这里我们可以做一些监听器(观察者模式)来搞定它。

可以看看c3p0 WrapperConnectionPoolDataSource,当修改了数据源配置项的时候,就会通知到Listener关联。

2个关注的接口:

  • VetoableChangeListener
     关注值是否可以拒绝修改
  • PropertyChangeListener
     关注值修改后的通知(反应)。

2个关注的support类

  • PropertyChangeSupport
  • VetoableChangeSupport
package com.demo.test;

import org.junit.*;

import java.beans.*;

/**
 * 测试VetoableChangeListener和PropertyChangeListener
 */
public class VetoableAndProChangTet {

    @org.junit.Test
    public void test() throws PropertyVetoException {
        VetoableChangeListener vcl = new VetoableChangeListener() {
            @Override
            public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
                Object newValue = evt.getNewValue();
                String propertyName = evt.getPropertyName();
                //reject规则 正则更好
                if("abc".equals(propertyName)){
                    throw new PropertyVetoException("",null);
                }
                System.out.println("property--->"+propertyName+",newValue--->"+newValue);
            }
        };
        PropertyChangeListener pcl = new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                Object newValue = evt.getNewValue();
                String propertyName = evt.getPropertyName();
                System.out.println("property--->"+propertyName+",newValue--->"+newValue);
            }
        };
        Subject s = new Subject();
        s.addPropertyChangeListener(pcl);
        s.addVetoableChangeListener(vcl);
        s.setContent("今天天气不错");
        s.setUserName("xiaohei");

    }

    /**
     * javabean
     */
    static class Subject{

        private String content="original content";  //constrained属性
        private String userName="original userName ";

        //support属性
        private VetoableChangeSupport vetoableChangeSupport = new VetoableChangeSupport(this);
        private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);


        public String getContent() {
            return content;
        }

        public void setContent(String content) throws PropertyVetoException {
            vetoableChangeSupport.fireVetoableChange("content",this.content,content);
            propertyChangeSupport.firePropertyChange("content",this.content,content);
            this.content = content;
        }

        public String getUserName() {
            return userName;
        }

        public void setUserName(String userName) {
            this.userName = userName;
        }


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

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

        @Override
        public String toString() {
            return "com.demo.test.VetoableAndProChangTet.Subject{" +
                    "userName='" + userName + '\'' +
                    ", content='" + content + '\'' +
                    '}';
        }
    }


}

相关文章

网友评论

      本文标题:constrained属性修改

      本文链接:https://www.haomeiwen.com/subject/xsredttx.html