lundi 26 mars 2018

Does observer design pattern method violate SRP when do 2 things: setting and notifying

I just read this sample code from the observer design pattern from this link. However, could anyone please explain whether the setState method is doing 2 things (setting the state and notifying the observers)?

If Yes, then does the method setState violate the single responsibility principle (SRP)?

If No, then how we could understand the SRP correctly? Thank you in advance.

public class Subject {
   private List<Observer> observers = new ArrayList<Observer>();
   private int state;

   public void setState(int state) {
     this.state = state;
     notifyAllObservers();
   }

   public void notifyAllObservers(){
     for (Observer observer : observers) {
        observer.update();
     }
   }

   // [...omitted other unrelated methods...]
}

Aucun commentaire:

Enregistrer un commentaire