Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Syntax: Who can an interface be an object?
    primarykey
    data
    text
    <p>I'm currently studying the Observer pattern and I've come across some confusion. </p> <p>When I read this line of code:</p> <pre><code>IObserverSubscribe user1= new ConcreteObserverYoutubeUser(); </code></pre> <p>I think the interface <code>IObserverSubscribe user1</code> is creating and instantiating <code>new ConcreteObserverYoutubeUser()</code>. That is a little confusing to me because usually the same class that is being declared is also instantiating. Should it be like this:</p> <pre><code>IObserverSubscribe user1= new IObserverSubscribe(); </code></pre> <p>Why is the interface decoration able to instantiate a another class?</p> <p>Full code below:</p> <p>Main:</p> <pre><code>package observerpattern; public class ObserverPattern { /** * The observer pattern is a software design pattern in which * an object, called the subject, maintains a list of its dependents, * called observers, and notifies them automatically of any * state changes, usually by calling one of their methods. * It is mainly used to implement distributed event handling systems. * The Observer pattern is also a key part in the familiar * Model View Controller (MVC) architectural pattern. */ public static void main(String[] args) { SubjectYouTubeChannel sytc= new SubjectYouTubeChannel();// create youtube channel IObserverSubscribe user1= new ConcreteObserverYoutubeUser(); IObserverSubscribe user2= new ConcreteObserverYoutubeUser(); IObserverSubscribe moderator1= new ConcreteObserverYoutubeModerator(); sytc.Subscribe(user1); sytc.Subscribe(user2); sytc.Subscribe(moderator1); sytc.Unsubscribe(user2); sytc.notifySubscribers(); } } </code></pre> <p>subject:</p> <pre><code>package observerpattern; import java.util.ArrayList; import java.util.List; import observerpattern.IObserverSubscribe; public class SubjectYouTubeChannel { private List&lt;IObserverSubscribe&gt; subscribers = new ArrayList&lt;IObserverSubscribe&gt;(); public void Subscribe(IObserverSubscribe ios){ subscribers.add(ios); } public void Unsubscribe(IObserverSubscribe ios){ subscribers.remove(ios); } public void notifySubscribers(){ for(IObserverSubscribe ios : subscribers ){ ios.Notify(); } } } </code></pre> <p>Interface Observer:</p> <pre><code>package observerpattern; public interface IObserverSubscribe { void Notify(); } </code></pre> <p>Concrete Observer:</p> <pre><code>package observerpattern; public class ConcreteObserverYoutubeUser implements IObserverSubscribe{ @Override public void Notify() { System.out.println("User watches video, comments, ect"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload