Attach observer observer public void detach observer

This preview shows page 37 - 42 out of 50 pages.

attach (Observer observer ){ observers.addElement(observer ); } public void detach (Observer observer) { observers. removeElement (observer ); } public void update () { for (Observer observer: observers) { observer.update (); } /* * For earlier versions of Java: * Observer tempObserver ; for (int i = 0; i < observers.size (); i++) { tempObserver = (Observer)observers.elementAt(i); tempObserver .update (); } */ } 35
3 ADDITIONAL EXAMPLES public String getState () { return subjectState ; } public void setState(String newState) { subjectState = newState; this .update (); } } public class Observer implements ObserverIF { private Subject subject; private String observerState ; public Observer(Subject subject) { this .subject = subject; this .subject.attach( this ); } public void update () { observerState = subject.getState (); } public String getState () { return observerState ; } } 36
3 ADDITIONAL EXAMPLES Example 3.4. Consider the class definitions below: public class Counter { private static String description = "A counter."; int element; public void click () { element ++; } public int getValue () { return element; } public void reset () { this .element = 0; } public static String getDescription () { return description; } } public interface Lockable { void lock (); void unlock (); boolean isLocked (); } public class LockableCounter extends Counter implements Lockable { static String description = "A lockable counter."; private boolean lock; public void lock () { this .lock = true ; } public void unlock () { this .lock = false ; } 37
3 ADDITIONAL EXAMPLES public boolean isLocked () { return this .lock; } public void reset () { this .element = this .element % 5; } public static String getDescription () { return description; } } public class LockableCounterTest { public static void main(String [] args ) { Counter lock1 = new LockableCounter (); Lockable lock2 = new LockableCounter (); System.out.println(lock1. getDescription ()); lock1.click (); lock2.click (); } } Our tasks are as follows: 1. Trace the body of the main method, and for each statement in that method describe the explicit responsibilities of the compiler and the runtime system. In case a statement fails compilation, consider it as being commented out. 2. What is the output of the program? Counter lock1 = new LockableCounter(); Compilation successful : The type of the expression on the right-hand side of the as- signment statement is a subtype of the type of the variable on the left-hand side. Additionally, the compiler validates the message new by locating a default constructor 38
3 ADDITIONAL EXAMPLES in class LockableCounter . Finally, the runtime system instantiates and initializes the object. Lockable lock2 = new LockableCounter(); Compilation successful : The reasoning is similar to the above. The runtime system instantiates and initializes the object. System.out.println(lock1.getDescription()); This is an example of hiding . The choice of static features is resolved based on the static type of the object. The static type of lock1 is Counter . The method will display A counter.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture