2014-08-30 11 views
0

我想要檢測何時將某些項目添加到數組列表或從中刪除某些項目。其實我有一個像下面的一些代碼:定製arraylist與通知刪除並添加

public class myClass { 

    MyCustomArrayList<MyObject> al; 

    public void method1() { 

     al.add(myObject);   
     // Do other works 
     al.remove(myObject) 
     // Do other works 
    } 

    private void DoByEachAdd() { 
     //I want array list call this method by adding each item to it. 
     // It should be in this class because it is doing some works 
     // related to this class. for example changing some private variables 
    } 

    private void DoByEachRemove() { 
     // I want array list call this method by adding each item to it. 
     // It should be in this class too. 
    } 

} 

我知道,數組列表,並沒有對具有監聽或某種形式的通知或事件的能力,如果我想檢測加應該有一個自定義的數組列表。類似下面的類:

class MyArrayList<T> { 
    private ArrayList<T> list; 

    public MyList(){ 
     list = new ArrayList<>(); 
    ... 
    } 
    public void add(T t) { 
     list.add(t) { 
     //do other things you want to do when items are added 
    } 
    public T remove(T t) { 
     list.remove(t); 
     //do other things you want to do when items are removed 
} 

(我從here得到它)

所以現在的問題是:我怎麼能通知MyArrayListal)對象調用DoByEachAddDoByEachRemove方法時remove和方法被解僱。有些身體有任何想法嗎?

回答

0

您可能需要使用Callback機制的訪問。

Java提供與使用以下步驟接口 回調機制:

  1. 接口與一個抽象方法 ,其名稱是已知的服務器定義。

  2. 每個有 服務器事件發生時要執行的操作的客戶端實現了接口,因此 給出了抽象方法的代碼。

  3. 當客戶端與服務器註冊,服務器 認爲指的是客戶端 一個實例變量和其類型爲接口類型。

  4. 服務器類通過調用 該客戶端的接口方法來調用客戶端操作。

請參閱下面的代碼並修改它以滿足您的要求: 的代碼是一個信用卡應用的仿真。該要求與您的要求類似,即在某些方法的調用中自動觸發某種方法。在以下代碼中,當調用changePin()方法時將自動調用pinChanged()方法。

public interface PinChangeListener { 
    public void pinChanged(); 
} 

public CreditCard { 
    public PinChangeListener pinChangeListener; 

    private int pin; 

    public changePin(int pin) { 
     this.pin = pin; 
     if (pinChangeListener != null) { 
      pinChangeListener.pinChanged(); 
     } 
    } 
} 

一個回調/監聽器連接到信用卡,你只需要實現PinChangeListener方法:

creditCard.pinChangeListener = new PinChangeListener() { 
    public void pinChanged() { 
     System.out.println("The pin has been changed"); 
    } 
}; 
0

你需要給你的myClassMyArrayList

class MyArrayList<T> { 
    private ArrayList<T> list; 
    private myClass theClass; 

public MyList(myClass theClass){ 
    list = new ArrayList<>(); 
    this.theClass = theClass; 
... 
} 
public void add(T t) { 
    list.add(t) { 
    //do other things you want to do when items are added 
    theClass.DoByEachAdd(); 
} 
public T remove(T t) { 
    list.remove(t); 
    //do other things you want to do when items are removed 
    theClass.DoByEachRemove 
} 

,並在您myClass給對象列表

public class myClass { 

    MyCustomArrayList<MyObject> al; 


public myClass(){ 

    al = new MyCustomArrayList<MyObject>(this); 

} 
public void method1() { 

    al.add(myObject);   
    // Do other works 
    al.remove(myObject) 
    // Do other works 
} 

public void DoByEachAdd() { 
    //I want array list call this method by adding each item to it. 
    // It should be in this class because it is doing some works 
    // related to this class. for example changing some private variables 
} 

public void DoByEachRemove() { 
    // I want array list call this method by adding each item to it. 
    // It should be in this class too. 
} 

} 
0

首先,按照命名規則。其次,您用於同一班級的三個班級名稱,MyList,MyArrayListMyCustomArrayList會混淆人。至於你的問題,你必須在myClass類型的MyArrayList之內有一個實例字段(除非你想重構DoByEachAddDoByEachRemovestatic)。這可以通過將其作爲構造參數添加來完成,例如,

// inside MyArrayList 
private ArrayList<T> list; 
private final myClass instance; 

public MyArrayList(myClass instance) { // <-- NOT MyList 
    list = new ArrayList(); 
    this.myClass = myClass; 
} 

此外,我質疑你的做法。具有MyArrayList實例的其他類只能使用addremove方法ArrayList。如果您想節省很多打擾並使所有方法都可見,請將list聲明爲public final或將MyArrayList聲明爲ArrayList的一個子類,例如,

public class MyArrayList<T> extends ArrayList<T> { 
    private final myClass instance; 

    public MyArrayList(myClass instance) { // <-- NOT MyList 
     list = new ArrayList(); 
     this.myClass = myClass; 
    } 

    @Override 
    public boolean add(T t) { 
     boolean returnThis = super.add(t); 
     // do some stuff 
     instance.DoByEachAdd(); 
     return returnThis; 
    } 

    @Override 
    public boolean remove(T t) { 
     boolean returnThis = super.remove(t); 
     // do some stuff 
     instance.DoByEachRemove(); 
     return returnThis; 
    } 
} 

如果你堅持能夠爲Tremove迴歸,宣告另一種方法:

public T removeT(T t) { 
    remove(t); 
    // do some stuff 
    return someT; 
}