I try to practice with the design patterns and explore one of the possible implementations of the Observer Design Pattern in Java. I paid attention, that in this example the object is defined with the interface type and not the class one:
//user-made observer interface
public interface Observer {…}
//observer class to watch after subject objects
public class MyTopicSubscriber implements Observer {…}
//subject class to notify observer of any updates
public class MyTopicPublisher implements Subject {
private List<Observer> observers;
}
As you can see, the list observers
is a type of Observer
, which is an interface. Why decided a programmer, in this case, to use the interface as a type for the list of objects and not the class name MyTopicSubscriber
?
The full code example published above:
http://www.journaldev.com/1739/observer-design-pattern-in-java-example-tutorial
Update to moderators:
In this question, I don't ask what exactly the interface is, but why it worths to use the interface as a type of collection's objects. Thus, my question can't be considered as a possible duplicate of the proposed question, which deals with basic explanation of what interfaces in programming are, my question is more narrow and specific.
Thanks.