???????????????????? Java ???????Java event notification??????????????????????????????????????Щ???塣????????????????????????£???С????????Щ????????
????Java ?????
?????????????????? Java Bean ?????????StateHolder???????????????е? int ?????? state ???????????????
????public class StateHolder {
????private int state;
????public int getState() {
????return state;
????}
????public void setState( int state ) {
????this.state = state;
????}
????}
??????????????????? Java bean ???????????????? ?????? ?????С????????????????????????????????????????????……
????// change event to broadcast
????public class StateEvent {
????public final int oldState;
????public final int newState;
????StateEvent( int oldState?? int newState ) {
????this.oldState = oldState;
????this.newState = newState;
????}
????}
????// observer interface
????public interface StateListener {
????void stateChanged( StateEvent event );
????}
????????????????????? StateHolder ?????????? StatListeners??
????public class StateHolder {
????private final Set<StateListener> listeners = new HashSet<>();
????[...]
????public void addStateListener( StateListener listener ) {
????listeners.add( listener );
????}
????public void removeStateListener( StateListener listener ) {
????listeners.remove( listener );
????}
????}
??????????????????????StateHolder#setState????????????????????б?????????????????????????????????β????仯???
????public void setState( int state ) {
????int oldState = this.state;
????this.state = state;
????if( oldState != state ) {
????broadcast( new StateEvent( oldState?? state ) );
????}
????}
????private void broadcast( StateEvent stateEvent ) {
????for( StateListener listener : listeners ) {
????listener.stateChanged( stateEvent );
????}
????}