Shivam Varshney
Shivam Varshney

Reputation: 55

Restrictive access in the Subject interface in the observer pattern

enter image description here

In this observer pattern, there is one more thing I am using: a push-pull mechanism, so there is a reference from observer to subject also.
so now when the Subject is part of the observer itself that observer can fetch the data it needs from the subject itself.
So it will create one problem in that the observer can also call other methods of the subject like notifyObserver(), and if I use an abstract class instead of an interface for the Subject then in that case a class that extends the Subject won't be able to extend other class as multiple inheritance not possible in java.
so what can I do so that the Observer can't call Subject's methods ?

Upvotes: 0

Views: 70

Answers (1)

jaco0646
jaco0646

Reputation: 17144

Simply remove the notifyObservers() method from the Subject interface. If the Observer shouldn't call that method, then the method shouldn't be a part of the interface seen by the Observer. That's fundamentally what the Interface Segregation Principle states.

Upvotes: 1

Related Questions