|
|
Line 6: |
Line 6: |
| * Modules are difficult to understand in isolation. | | * Modules are difficult to understand in isolation. |
| * Modules are difficult to reuse or test because dependent modules must be included. | | * Modules are difficult to reuse or test because dependent modules must be included. |
|
| |
| = The Observer pattern =
| |
| as an example in Lecture 23. But we've really only scratched the surface on what there is to know about achieving low coupling.
| |
|
| |
| Browse the Web and the ACM DL for other information, both
| |
|
| |
| = Observer Pattern Example =
| |
|
| |
| Here is an example that takes keyboard input and treats each input line as an event. The example is built upon the library classes <tt>java.util.Observer</tt> and <tt>java.util.Observable</tt>. When a string is supplied from System.in, the method <tt>notifyObserver</tt> is then called, in order to notify all observers of the event's occurrence, in the form of an invocation of their 'update' methods - in our example, <tt>ResponseHandler.update(...)</tt>.
| |
|
| |
| The file myapp.java contains a main() method that might be used in order to run the code.
| |
|
| |
| /* File Name : EventSource.java */
| |
|
| |
| package OBS;
| |
| import java.util.Observable; //Observable is here
| |
| import java.io.BufferedReader;
| |
| import java.io.IOException;
| |
| import java.io.InputStreamReader;
| |
|
| |
| public class EventSource extends Observable implements Runnable
| |
| {
| |
| public void run()
| |
| {
| |
| try
| |
| {
| |
| final InputStreamReader isr = new InputStreamReader( System.in );
| |
| final BufferedReader br = new BufferedReader( isr );
| |
| while( true )
| |
| {
| |
| final String response = br.readLine();
| |
| setChanged();
| |
| notifyObservers( response );
| |
| }
| |
| }
| |
| catch (IOException e)
| |
| {
| |
| e.printStackTrace();
| |
| }
| |
| }
| |
| }
| |
| </source>
| |
|
| |
| /* File Name: ResponseHandler.java */
| |
|
| |
| package OBS;
| |
|
| |
| import java.util.Observable;
| |
| import java.util.Observer; /* this is Event Handler */
| |
|
| |
| public class ResponseHandler implements Observer
| |
| {
| |
| private String resp;
| |
| public void update (Observable obj, Object arg)
| |
| {
| |
| if (arg instanceof String)
| |
| {
| |
| resp = (String) arg;
| |
| System.out.println("\nReceived Response: "+ resp );
| |
| }
| |
| }
| |
| }
| |
| </source>
| |
|
| |
| /* Filename : myapp.java */
| |
| /* This is main program */
| |
|
| |
| package OBS;
| |
|
| |
| public class myapp
| |
| {
| |
| public static void main(String args[])
| |
| {
| |
| System.out.println("Enter Text >");
| |
|
| |
| // create an event source - reads from stdin
| |
| final EventSource evSrc = new EventSource();
| |
|
| |
| // create an observer
| |
| final ResponseHandler respHandler = new ResponseHandler();
| |
|
| |
| // subscribe the observer to the event source
| |
| evSrc.addObserver( respHandler );
| |
|
| |
| // starts the event thread
| |
| Thread thread = new Thread(evSrc);
| |
| thread.start();
| |
| }
| |
| }
| |
| </source>
| |
|
| |
| Also refer to an article in JavaWorld
| |
| http://www.javaworld.com/javaworld/javaqa/2001-05/04-qa-0525-observer.html
| |
|
| |
|
| |
|
|
| |
|
| [[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]] | | [[CSC/ECE 517 Summer 2008/wiki3 Assignment|Back to the assignment page]] |