CSC/ECE 517 Summer 2008/wiki3 3 cd

From Expertiza_Wiki
Revision as of 02:15, 27 July 2008 by Cadedam (talk | contribs)
Jump to navigation Jump to search

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();
        }
    }
}
/* 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 );
        }
    }
}
/* 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();
    }
}

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.