2011-11-30 76 views
3

有什麼辦法可以檢測整數變化嗎?比如創建一個偵聽器來偵聽整數來檢測並更改它的值。我知道這可能與布爾有一些技巧,但我似乎無法適應這個int值。有沒有人有任何想法如何做到這一點?我需要知道如何在Java語言中執行此操作。下面是我在網上找到的允許布爾型監聽器的代碼。我怎樣才能將其轉換爲整數監聽器?檢測整數變化

import java.util.List; 
import java.util.ArrayList; 
import java.util.EventListener; 
import java.util.EventObject; 
import java.awt.EventQueue; 

//can u see this austin? can u see this i typed this at 9:33 my time 
/** 
* This class uses the EventQueue to process its events, but you should only 
* really do this if the changes you make have an impact on part of a GUI 
* eg. adding a button to a JFrame. 
* 
* Otherwise, you should create your own event dispatch thread that can handle 
* change events 
*/ 
public class test1 implements BooleanChangeDispatcher { 

    public static void main(String[] args) { 

     BooleanChangeListener listener = new BooleanChangeListener() { // add this to the class 
      @Override 
      public void stateChanged(BooleanChangeEvent event) { 
       System.out.println("Detected change to: " 
        + event.getDispatcher().getFlag() 
        + " -- event: " + event); 
      } 
     }; 

     test1 test = new test1(false); 
     test.addBooleanChangeListener(listener); 

//  test.setFlag(false); // no change, no event dispatch 
//  test.setFlag(true); // changed to true -- event dispatched 

    } 

    private boolean flag; 
    private List<BooleanChangeListener> listeners; 

    public test1(boolean initialFlagState) { 
     flag = initialFlagState; 
     listeners = new ArrayList<BooleanChangeListener>(); 
    } 

    @Override 
    public void addBooleanChangeListener(BooleanChangeListener listener) { 
     listeners.add(listener); 
    } 

    @Override 
    public void setFlag(boolean flag) { 
     if (this.flag != flag) { 
      this.flag = flag; 
      dispatchEvent(); 
     } 
    } 

    @Override 
    public boolean getFlag() { 
     return flag; 
    } 

    private void dispatchEvent() { 
     final BooleanChangeEvent event = new BooleanChangeEvent(this); 
     for (BooleanChangeListener l : listeners) { 
      dispatchRunnableOnEventQueue(l, event); 
     } 
    } 

    private void dispatchRunnableOnEventQueue(
       final BooleanChangeListener listener, 
       final BooleanChangeEvent event) { 

     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       listener.stateChanged(event); 
      } 
     }); 
    } 

} 

interface BooleanChangeDispatcher { 

    public void addBooleanChangeListener(BooleanChangeListener listener); 
    public boolean getFlag(); 
    public void setFlag(boolean flag); 

} 

/** 
* Listener interface for classes interested in knowing about a boolean 
* flag change. 
*/ 
interface BooleanChangeListener extends EventListener { 

    public void stateChanged(BooleanChangeEvent event); 

} 

/** 
* This class lets the listener know when the change occured and what 
* object was changed. 
*/ 
class BooleanChangeEvent extends EventObject { 

    private final BooleanChangeDispatcher dispatcher; 

    public BooleanChangeEvent(BooleanChangeDispatcher dispatcher) { 
     super(dispatcher); 
     this.dispatcher = dispatcher; 
    } 

    // type safe way to get source (as opposed to getSource of EventObject 
    public BooleanChangeDispatcher getDispatcher() { 
     return dispatcher; 
    } 
} 
+1

你有一個首選語言? –

+0

我需要知道的Java – MrDrProfessorTyler

+2

這是一個整數是從一個地方更改?你怎麼用布爾值來做到這一點? – birryree

回答

2

我會創建一個能夠註冊監聽器的類。下面是一個模擬的例子。它甚至可以按原樣編譯(假設你編寫相應的VocalIntegerListener接口存在並以某種方式實現......這很簡單)。

class VocalInteger { 

    private int value; 
    private final Object lock = new Object(); 
    Set<VocalIntegerListener> listeners; // assume interface exists - it's easy 


    public VocalInteger() { 
     this(0); 
    } 

    public VocalInteger(int value) { 
     this.value = value; 
     listeners = new HashSet<VocalIntegerListener>(); 
    } 

    public int getValue() { 
     return value; 
    } 

    public void setValue(int value) { 
     synchronized(lock) { 
      int oldValue = this.value; 
      this.value = value; 
      for(VocalIntegerListener listener : listeners) { 
       listener.fireChangedEvent(oldvalue, value); // assume exists 
      } 
     } 
    } 

    public void registerListener(VocalIntegerListener listener) { 
     synchronized(lock) { 
      listeners.add(listener); 
     } 
    } 
} 
+0

嗯,我想我問如何創建這樣一個接口? – MrDrProfessorTyler

+0

其中一些可以通過['Observable'](http://docs.oracle.com/javase/7/docs/api/java/util/Observable.html)完成(如果人們還在使用該類)。 –

+1

@glowcoder:哎。我的JCIP書正在哭泣。如果不同的線程用於註冊監聽器並調用* setValue *? ; )我的意思是,對於聽衆:*列表*和*新的CopyOnWriteArrayList *,肯定...但*設置*和不同步*添加*?哎喲。只是ouch。 :) – TacticalCoder