title: Java alternative Observer Pattern
body: <p>In my Object Oriented Lecture at the University I encountered a strange Observer-Pattern which differs from the ones which I see online.</p>
  
  <p>Instead of seperating the Observable and Observer in two different Objects there is one big Object which combines the two. Additionally, the Superobject got a method, which returns the observer (as a handle).</p>
  
  <p>My lecturer told me that this way of implementing the pattern would solve an architectural issue which would cause the update-method to explode if we extend it. I think I get most of his intention.</p>
  
  <p>My issue right now is, that I got no other source to learn about this 'alternative' Observer-Pattern, or at least I'm not able to find an article about the pattern. Though I think I get the point of my lecturer, I don't think I fully understand this patttern, i.e. the benefits of compress all classes together.</p>
  
  <p>Can anybody elaborate, why this architecture would benefit my code? Or why can't I simply extract the logic of the update-method out into an utility class?</p>
  
  <p>Below is the (java) class which we built. I deleted some of the unnecessary logic.</p>
  
  <pre><code>import java.util.Observable;
  import java.util.Observer;
  import java.util.concurrent.Semaphore;
  
  public class Server implements InterfaceServer {
      public Client client;
      private final ObjServer oServer;
  
      public Server() {
          oServer = new ObjServer();
          new Thread(server).start();
      }
  
      public Client connect() throws InterruptedException {
          Client handle = new Client();
          oServer.addObserver(handle);
          return handle;
      }
  
      public class ObjServer extends Observable implements Runnable {
  
          @Override
          public void run() {
              while (true) {
                  // pull data
  
                  setChanged();
                  notifyObservers(m);
              }
          }
      }
  
      public static class Client implements Observer {
  
          private Object obj;
          private Semaphore semaphore = new Semaphore(0, true);
  
          public Object getObject() throws InterruptedException {
              semaphore.acquire();
              System.out.println(obj);
  
              return obj;
          }
  
          @Override
          public void update(Observable o, Object arg) {
              obj = arg;
              semaphore.release();
          }
  
      }
  
      public static void main(String... args) throws Exception {
          Server server = new Server();
  
          Client client = new Client();
  
          client = server.connect();
  
          while (true) {
              client.getObject();
          }
      }
  }
  </code></pre>