Skip to main content
Post Closed as "Duplicate" by Telastyn, durron597, CommunityBot, gnat
explanation to moderators regarding possible duplicate of the proposed question
Source Link
Mike
  • 135
  • 6

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? Is there any idea behind of the use of such design?

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.

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? Is there any idea behind of the use of such design?

The full code example published above:
http://www.journaldev.com/1739/observer-design-pattern-in-java-example-tutorial

Thanks.

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.

deleted 5 characters in body
Source Link
Mike
  • 135
  • 6

I try to practice with the design patterns and explore one of the possible implementations of the Observer Design PatternObserver 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 the 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, a programmer decided to use the interface as a type for the list of objects and not the class name MyTopicSubscriber? Is there any idea behind of the use of such design?

The full code example published above:
http://www.journaldev.com/1739/observer-design-pattern-in-java-example-tutorial

Thanks.

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 the 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 in this case, a programmer decided to use the interface as a type for the list of objects and not the class name MyTopicSubscriber? Is there any idea behind of the use of such design?

The full code example published above:
http://www.journaldev.com/1739/observer-design-pattern-in-java-example-tutorial

Thanks.

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? Is there any idea behind of the use of such design?

The full code example published above:
http://www.journaldev.com/1739/observer-design-pattern-in-java-example-tutorial

Thanks.

Source Link
Mike
  • 135
  • 6

Define an object with the interface as a type instead of class name

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 the 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 in this case, a programmer decided to use the interface as a type for the list of objects and not the class name MyTopicSubscriber? Is there any idea behind of the use of such design?

The full code example published above:
http://www.journaldev.com/1739/observer-design-pattern-in-java-example-tutorial

Thanks.