Observer and Observable

alkathirikhalid

Using JAVA Implementation

Message Board (Observable)

package com.alkathirikhalid.demo;

import java.util.Observable;

/**
  * <p>
  * Message Board as Observable.</p>
  * <p>
  * It can be observed for new message changes, it can also have one or more
  * observers, when an observable object is newly created, its set of observers
  * is always empty at first.</p>
  *
  * @author alkathirikhalid
  */
public class MessageBoard extends Observable {

    /**
      * The message on the Message Board
      */
     private String message;

    /**
      * Getter
      *
      * @return message Board
      */
     public String getMessage() {
         return message;
     }

    /**
      * Setter
      *
      * @param message Board
      */
     public void setMessage(String message) {
         this.message = message;
         // Marks this Observable object as having been changed
         setChanged();
         // notify all of Message Board observers
         notifyObservers();
     }
}

User (Observer)

package com.alkathirikhalid.demo;

import java.util.Observable;
import java.util.Observer;

/**
  * <p>
  * User as Observer.</p>
  * <p>
  * Gets updated messages based on changes in observable Message Board.</p>
  *
  * @author alkathirikhalid
  */
public class User implements Observer {

    /**
      * Observable Message Board
      */
     private final MessageBoard messageBoard;
     /**
      * User In box message
      */
     private String inbox;

    /**
      * Constructor
      *
      * @param messageBoard the user is Observing
      */
     public User(MessageBoard messageBoard) {
         this.messageBoard = messageBoard;
     }

    /**
      * Getter
      *
      * @return in box message
      */
     public String getInbox() {
         return inbox;
     }

    /**
      * Setter
      *
      * @param inbox message
      */
     public void setInbox(String inbox) {
         this.inbox = inbox;
     }

    @Override
     public void update(Observable o, Object arg) {
         // Confirms the Observable is a Message Board Object
         if (o == messageBoard) {
             // Sets the user in box message as the incoming new message board
             setInbox(messageBoard.getMessage());
             // Prints to screen
             System.out.println(getInbox());
         }
     }
}

Test Application

package com.alkathirikhalid.demo;

/**
  *
  * @author alkathirikhalid
  */
public class TestApp {

    /**
      * @param args the command line arguments
      */
     public static void main(String[] args) {
         // New Message Board Instance
         MessageBoard messageBoard = new MessageBoard();
         // New User Instance Observing Message Board
         User user = new User(messageBoard);
         // Add User as an Observer of Message Board
         messageBoard.addObserver(user);
         // Update the Message Board with new Message
         messageBoard.setMessage(“New Message!”);
     }

}

Writing Your Own Implementation

Message Board Listener Interface

package com.alkathirikhalid.demo;

/**
  *
  * @author alkathirikhalid
  */
public interface MessageBoardListener {

    void update(String message);
}

Message Board Class

package com.alkathirikhalid.demo;

public class MessageBoard {

    private MessageBoardListener messageBoardListener;
     /**
      * The message on the Message Board
      */
     private String message;

    public void setMessageBoardListener(MessageBoardListener messageBoardListener) {
         this.messageBoardListener = messageBoardListener;
     }

    /**
      * Getter
      *
      * @return message Board
      */
     public String getMessage() {
         return message;
     }

    /**
      * Setter
      *
      * @param message Board
      */
     public void setMessage(String message) {
         this.message = message;
         // notify all of Message Board listeners
         messageBoardListener.update(message);
     }
}

User

package com.alkathirikhalid.demo;

public class User implements MessageBoardListener {

    private String inbox;

    public String getInbox() {
         return inbox;
     }

    public void setInbox(String inbox) {
         this.inbox = inbox;
     }

    @Override
     public void update(String message) {
         setInbox(message);
         // Prints to screen
         System.out.println(getInbox());
     }
}

Test Application

package com.alkathirikhalid.demo;

/**
  *
  * @author alkathirikhalid
  */
public class TestApp {

    /**
      * @param args the command line arguments
      */
     public static void main(String[] args) {
         // New Message Board Instance
         MessageBoard messageBoard = new MessageBoard();
         // New User Instance
         User user = new User();
         // Add User as a Listening to the Message Board
         messageBoard.setMessageBoardListener(user);
         // Update the Message Board with new Message
         messageBoard.setMessage(“New Message!”);
     }

}

Result

The same for both implementations

New Message!

About alkathirikhalid

"Knowledge without experience is just information."
This entry was posted in Programming, Web Application, Web Developement. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s