2014-08-30 2 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에서 그것을 얻을)

그래서 문제가 있습니다 : 내가 MyArrayList (al)의 목적을 알릴 수있는 방법을 DoByEachAddDoByEachRemove 방법을 호출 할 때 removeadd 메서드가 실행되었습니다. 어떤 시체에 아이디어가 있습니까?

답변

0

에 개체를 제공하여 myClass

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 
} 

MyArrayList에와에 대한 액세스를 제공해야합니다. 인터페이스는 이름이 서버로 알려진 방법 추상적으로 정의

  1. :

    자바는 다음 단계를 사용하여 인터페이스 와 콜백 메카니즘을 제공한다.

  2. 서버 이벤트가 발생할 때 수행 할 작업이있는 각 클라이언트는 인터페이스를 구현하므로 추상 메서드에 대한 코드를 제공합니다.

  3. 클라이언트가 서버에 등록 할 때 서버 은 클라이언트 을 참조하고 유형이 인터페이스 유형 인 인스턴스 변수를 보유합니다.

  4. 서버 클래스는 해당 클라이언트의 인터페이스 메소드 인 을 호출하여 클라이언트 작업을 호출합니다.

다음 코드를 참조하여 요구 사항에 맞도록 수정 : 코드는 신용 카드 응용 프로그램 시뮬레이션을위한 것입니다. 요구 사항은 사용자의 것과 유사합니다. 즉, 일부 메서드는의 호출에서 자동으로 트리거됩니다. pinChanged() 메서드는 changePin() 메서드가 호출 될 때 자동으로 호출됩니다.

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

당신은 당신의 myClass 목록 당신은 아마 Callback 메커니즘을 사용해야합니다

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은 사람들을 혼란스럽게합니다. 귀하의 질문에 및 DoByEachRemovestatic으로 리팩토링하지 않으려면 유형의 MyArrayList 안에 인스턴스 필드가 있어야합니다. 이를 생성자 매개 변수로 추가하여이를 수행 할 수 있습니다.

// 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을 사용할 수 있습니다. 많은 번거 로움을 덜고 모든 메소드를 표시하려면 을 public final으로 지정하거나 MyArrayListArrayList의 하위 클래스로 지정하십시오.

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; 
}