일부 항목을 배열 목록에 추가하거나 항목을 제거 할 때 감지하고 싶습니다. 사실은 내가 몇 가지 코드 아래와 같이 있습니다삭제 알림 추가 사용자 정의 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에서 그것을 얻을)
그래서 문제가 있습니다 : 내가 MyArrayList
(al
)의 목적을 알릴 수있는 방법을 DoByEachAdd
및 DoByEachRemove
방법을 호출 할 때 remove
및 add
메서드가 실행되었습니다. 어떤 시체에 아이디어가 있습니까?