title: Interpretation of an interface driven implementation of the observer pattern in Java
body: <p>I am studying the <strong>observer pattern</strong> implemented in Java and I have some doubts about how the tutorial implementation exactly works.</p>
  
  <p>I know that this pattern is used in all the situation that expect the user interaction with a GUI (for example the user click on a button) or all the situation in which an event happen at an unpredictable time (like a timer, when the timer snap an event have to be handled).</p>
  
  <p>I know that in the observer pattern are involved 2 different type of objects:</p>
  
  <ol>
  <li><p>The <strong>subject</strong> object: that is <strong>the object on which something happen at an unpredictable time</strong> (for example a button that could be clicked in any time by the user)</p></li>
  <li><p>The <strong>observer</strong> object (or <strong>listener</strong>): it seems to me that it contains the code that is performed when something change into the <strong>subject</strong> object (for example it handle the click of my button). Is it true or am I missing something? I am absolutely not true about this statement...I have the doubt that maybe the <strong>observer</strong> object have only to listen the change on the <strong>subject</strong> and perform an operation on the subject when this change happens. For example: the button (the <strong>subject</strong>) is clicked, the observer listen to it and discover this change and it performs a specific operation on the subject (perform a specific method on the subject object).</p></li>
  </ol>
  
  <p>What is the correct interpretation? The observer listen the subject change and perform an operation defined into the observer or perform an operation defined into the subject?</p>
  
  <p>I know that to do it I can pass a reference of the <strong>subject</strong> to the <strong>observer</strong> (I think that the operation performed is defined into the subject.) but this is a <strong>bad practice</strong> because it involve a form of <strong>tight coupling</strong> between subject and observer. I know that an interface driven solution exists but I have some difficulties figuring it out.</p>
  
  <p>The tutorial make this example that represent a view that use Swing to render 2 buttons:</p>
  
  <pre><code>package com.caveofprogramming.designpatterns.demo1.view;
  
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  
  import com.caveofprogramming.designpatterns.demo1.model.Model;
  
  public class View extends JFrame implements ActionListener {
  
      private Model model;
  
      private JButton helloButton;        // SUBJECT OBJECT: l'oggetto su cui avviene l'evento
      private JButton goodbyeButton;      // SUBJECT OBJECT: l'oggetto su cui avviene l'evento
  
      public View(Model model) {
          super("MVC Demo");
  
          this.model = model;
  
          // Crea i 2 bottoni definiti sopra:
          helloButton = new JButton("Hello!");
          goodbyeButton = new JButton("Goodbye!");
  
          // Setta il layout manager da usare che stabilisce come sono posizionati gli elementi:
          setLayout(new GridBagLayout());
  
          GridBagConstraints gc = new GridBagConstraints();
          gc.anchor = GridBagConstraints.CENTER;
          gc.gridx=1;
          gc.gridy=1;
          gc.weightx=1;
          gc.weighty=1;
          gc.fill=GridBagConstraints.NONE;
  
          add(helloButton, gc);   // Aggiunge helloButton dentro il layout
  
          gc.anchor = GridBagConstraints.CENTER;
          gc.gridx=1;
          gc.gridy=2;
          gc.weightx=1;
          gc.weighty=1;
          gc.fill=GridBagConstraints.NONE;
  
          add(goodbyeButton, gc); // Aggiunge goodbyeButton dentro il layout
  
          helloButton.addActionListener(this);
          goodbyeButton.addActionListener(this);
  
          goodbyeButton.addActionListener(new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent e) {
                  System.out.println("Sorry to see you go.");
              }
  
          });
  
          setSize(600, 500);                          // Setta le dimensioni della finestra
          setDefaultCloseOperation(EXIT_ON_CLOSE);    // Setta il comportamento di cosa succede premendo il tasto X
          setVisible(true);                           // Setta la finestra come visibile
      }
  
      @Override
      public void actionPerformed(ActionEvent e) {
  
          JButton source = (JButton)e.getSource();
  
          if(source == helloButton) {
              System.out.println("Hello there!");
          }
          else {
              System.out.println("Some other button.");
          }
  
      }   
  
  }
  </code></pre>
  
  <p>In this example I have 2 buttons that represent my <strong>subject</strong> objects, these:</p>
  
  <pre><code>private JButton helloButton;        
  private JButton goodbyeButton;
  </code></pre>
  
  <p>The <strong>View</strong> class implements an <strong>ActionListener</strong> interface (That it is the interface mentioned earlier?)</p>
  
  <p>Looking inside the <strong>ActionListener.class</strong> I found:</p>
  
  <pre><code>// Method descriptor #8 (Ljava/awt/event/ActionEvent;)V
  public abstract void actionPerformed(java.awt.event.ActionEvent arg0);
  </code></pre>
  
  <p>This is a method that has to be implemented and this is the method that is invoked when an action occurs and I have to implement it, this:</p>
  
  <pre><code>@Override
  public void actionPerformed(ActionEvent e) {
  
      JButton source = (JButton)e.getSource();
  
      if(source == helloButton) {
          System.out.println("Hello there!");
      }
      else {
          System.out.println("Some other button.");
      }
  
  }
  </code></pre>
  
  <p>The <strong>actionPerformed()</strong> method is implemented into the <strong>View</strong> class because it extend the <strong>ActionListener</strong> interface and it handle the click of the buttons.</p>
  
  <p>Is the <strong>ActionListener</strong> interface implementation my <strong>observer</strong> object? And it means that the <strong>View* class contains the **subject</strong> objects (my buttons) but it is also a <strong>observer/listener</strong> implementation?</p>
  
  <p>To add the <strong>listerner</strong> (the <strong>observer</strong> object) to a button I do:</p>
  
  <pre><code>helloButton.addActionListener(this);
  </code></pre>
  
  <p>and I pass <strong>this</strong> because the class that contains the <strong>observer</strong> is the same of the one where the <strong>subject</strong> objects are defined. Is it correct?</p>
  
  <p>It is a best practice because I bundle together the subject with the observer into a single class and I have not tight coupling derived by a reference of the subject inside the observer?</p>
  
  <p>Is it my reasoning correct or am I missing something?</p>