From Design Pattern by Gang of Four
Why are field observers
, and methods Attach
, Detach
and Notify
are in the interface Subject
, while methods GetState
and SetState
and field SubjectState
are in the concrete subclass ConcreteSubject
? Why are the members distributed in the subject interface and concrete subclass as they are?
Why is method Update
in the interface Observer
, while fields subject
and observerState
in the concrete subclass ConcreteObserver
?
Why are the members distributed in the observer interface and concrete subclass as they are?
There doesn't seem to be symmetry between subject and observer.
For example, why does field observers
belong to interface Subject
, while field subject
belong to ConcreteObserver
?
Thanks.
The method GetState and SetState in the Subject interface would make Subject interface dependent on how the dependent objects are updated. This is a violation of Dependency Inversion principle.
All Subject interface does is attach a concrete subject and notifies the observer. How the states of the concrete objects are changing it is independent of that. This is the reason why getState and setState not part of the Subject Interface.
Similar reason why observeState is not part of Observer interface.