Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Think about what an interface does and what the observer pattern is meant to be used for.</p> <p>ConcreteObserverYoutubeUser implements the IObserverSubscribe interface because it needs to guarantee that it has the "Notify()" method. All classes that are supposed to be "IObserverSubscribers" therefore need to do something in particular when they are notified.</p> <p>If you could do the following, it would defeat the purpose of the observer pattern:</p> <pre><code> IObserverSubscribe user1= new IObserverSubscribe(); </code></pre> <p>A youtube moderator and a youtube user have different capabilities so you cannot use the same Notify method for both (see modified code below): </p> <p>User Class:</p> <pre><code> package observerpattern; public class ConcreteObserverYoutubeUser implements IObserverSubscribe{ @Override public void Notify() { sendEmail("A new video was added! It might be a cat video, so you should " + "probably view it, vote on it, comment on it, or ignore it."); } @Override public void watchVideo(Video v) { //... } @Override public void giggleAtCatVideo() { //.. } } </code></pre> <p>Moderator class: </p> <pre><code> package observerpattern; public class ConcreteObserverYoutubeModerator implements IObserverSubscribe{ @Override public void Notify() { sendEmail("New video added, see if it should be deleted or if there is copyright infringement."); } @Override public void deleteVideo(Video v) { //.. } } </code></pre> <p>Why is that useful? Well because you can make another class (in this case, SubjectYouTubeChannel) that can hold objects that implement the "IObserverSubscribe" interface. When something about that subject changes you can notify every observer that's associated with that subject so that all the observers will know something has changed. </p> <p>A better example would be if SubjectYouTubeChannel had an "addVideo" method (see below) and the notify method in the moderator class and user class sent different emails to them alerting them to the change.</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(); } } public void addVideo(Video v){ //... video add code notifySubscribers(); } } </code></pre> <p>This means you can do cool things like this:</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(); Video v = new Video() //imagine that class exists sytc.Subscribe(user1); sytc.Subscribe(user2); sytc.Subscribe(moderator1); sytc.Unsubscribe(user2); sytc.addVideo(v); //notifySubscribers() is called within SubjectYouTubeChannel's addVideo() so //all users and moderators who subscribe to the channel have been notified with //messages corresponding to what they should do } } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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