CSC/ECE 517 Summer 2008/wiki3 3 cd: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 19: Line 19:
The file myapp.java contains a main() method that might be used in order to run the code.
The file myapp.java contains a main() method that might be used in order to run the code.


<source lang="java">
  /* File Name : EventSource.java */
  /* File Name : EventSource.java */
   
   
Line 51: Line 50:
  </source>
  </source>


<source lang="java">
  /* File Name: ResponseHandler.java */
  /* File Name: ResponseHandler.java */
   
   
Line 73: Line 71:
  </source>
  </source>


<source lang="java">
  /* Filename : myapp.java */
  /* Filename : myapp.java */
  /* This is main program */
  /* This is main program */

Revision as of 02:18, 27 July 2008

Low coupling.

Coupling in Software Design can be either High or Low. What this means is that object and classes in your code can exist indepently of other objects or classes. For example a system that has high coupling might experience the following issues:

  • Change in one module forces a ripple of changes in other modules.
  • Modules are difficult to understand in isolation.
  • 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

Java

Here is an example that takes keyboard input and treats each input line as an event. The example is built upon the library classes java.util.Observer and java.util.Observable. When a string is supplied from System.in, the method notifyObserver 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, ResponseHandler.update(...).

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

theoretical

practical

and produce a guide to what there is to know about low coupling. Be sure to

highlights

those aspects that would be appropriate for inclusion in CSC/ECE 517.