CS 535: Object-Oriented Programming & Design |
---|
class LinkedListCoupledToStandardOut { public void print() { System.out.println( "Here is the list" ); // add code here to print out list to standard out } //other methods of LinkedList not shown }
class LinkedListCoupledToStream { public void print(Writer out) { out.write( "Here is the list" ); // add code here to print out list to out } //other methods of LinkedList not shown } class PrintingListsToTwoTypesOfOutput { public static void main( String args[] ) throws IOException { LinkedListCoupledToStream list = new LinkedListCoupledToStream(); //pretend I added elements to the list OutputStreamWriter outConverter = new OutputStreamWriter( System.out ); list.print( outConverter ); Writer out = new BufferedWriter( new FileWriter( "ListFile")); list.print( out ); }
class LinkedListCoupledToString { public String toString() { StringBuffer listBuffer = new StringBuffer(); // add code here to append list elements to the buffer return listBuffer.toString(); } //other methods of LinkedList not shown } class Test { public static void main( String args[] ) throws IOException { LinkedListCoupledToString list = new LinkedListCoupledToString(); //pretend I added elements to the list System.out.println( list.toString() ); Writer out = new BufferedWriter( new FileWriter( "ListFile")); out.write( list.toString()); }
class BadCounterExample extends Frame { // create buttons for window Button increase = new Button( "Increase" ); Button decrease = new Button( "Decrease" ); Button reset = new Button( "Reset" ); int count = 0; public BadCounterExample( int width, int height ) { // Set up a window setTitle( "Button Example" ); resize( width, height ); setLayout( new FlowLayout() ); add( increase ); add( decrease ); add( reset ); show(); } public boolean action( Event processNow, Object argument ) { // Respond to user pushing a button if ( processNow.target == increase ) count++; else if ( processNow.target == decrease ) count--; else if ( processNow.target == reset ) count = 0; else return false; repaint(); return true; } public void paint( Graphics display ) { // Draw in window display.drawString( "The count is " + count , 50, 50 ); } public static void main( String args[] ){ new BadCounterExample( 200, 100 ); } }
class Counter { private int count = 0; public void increase() { count++; } public void decrease() { count--; } public void reset() { count = 0; } public int value() { return count; } public String toString() { return String.valueOf( count ); } }
class SimpleCounter extends Observable{ private int count = 0; public void increase() { count++; setChanged(); notifyObservers(); } } class SimpleObserver implements Observer { String id; public SimpleObserver( String id ) { this.id = id ; } public void update( Observable sender, Object message ) { System.out.println( "From " + id + " New value " + ((SimpleCounter) sender).count ); } } class ObserveTest { public static void main( String args[] ) { SimpleCounter test = new SimpleCounter(); SimpleObserver a = new SimpleObserver( "a" ); test.addObserver( a); test.addObserver( new SimpleObserver( "b" ) ); test.increase(); test.addObserver( new SimpleObserver( "c" ) ); test.increase(); } }
class ObserveTest { public static void main( String args[] ) { SimpleCounter test = new SimpleCounter(); SimpleObserver a = new SimpleObserver( "a" ); test.addObserver( a); test.addObserver( new SimpleObserver( "b" ) ); test.increase(); test.addObserver( new SimpleObserver( "c" ) ); test.increase(); } }
class ObserveTest { public static void main( String args[] ) { SimpleCounter test = new SimpleCounter(); SimpleObserver a = new SimpleObserver( "a" ); test.addObserver( a); test.addObserver( a ); test.increase(); test.addObserver( a ); test.increase(); } }
class ObserveTest { public static void main( String args[] ) { SimpleCounter test = new SimpleCounter(); SimpleObserver a = new SimpleObserver( "a" ); SimpleObserver b = new SimpleObserver( "b" ); test.addObserver( a); test.addObserver( b); test.increase(); System.out.println( "Delete a" ); test.deleteObserver( a ); test.increase(); System.out.println( "Delete a again" ); test.deleteObserver( a ); test.increase(); System.out.println( "Delete all" ); test.deleteObservers( ); test.increase(); } }
class SimpleCounter extends Observable{ private int count = 0; public void increase() { count++; setChanged(); notifyObservers( "Hi Mom" ); } } class SimpleObserver implements Observer { String id; public SimpleObserver( String id ) { this.id = id ; } public void update( Observable sender, Object message ) { System.out.println( "From " + id + " With message: " + message); } } class ObserveTest { public static void main( String args[] ) { SimpleCounter test = new SimpleCounter(); SimpleObserver a = new SimpleObserver( "a" ); test.addObserver( a); test.increase(); test.increase(); } }
class SimpleCounter { private int count = 0; private Observable myObservers = new Observable(); public void addObserver( Observer newObserver ) { myObservers.addObserver( newObserver ); } public void increase() { count++; changeNotify(); } protected void changeNotify() { myObservers.setChanged(); myObservers.notifyObservers( ); } }