Observer Pattern

1.观察者模式

public interface Subject{

public void attach(Observer observer);

public void detach(Observer observer);

}

public class MySubject implements Subject{

private Map observers =new HashMap();

public void attach(Observer observer){

  this.observer.put("...",observer);

}

public void detach(Observer observer){

  this.remove("...");

}

 

public void notifyObserver(){

   for(int i = 0 ; i < observers.size();i++){

      Observer observer = observer.get(i);

      observer.update();

  }

}

}

 

public interface Observer{

   public void update();

}

 

你可能感兴趣的:(observer)