Cust wants to know:
"How can we clone an Observable?"
Problem is that Observable doesn't implement it's own Clone() method;
Cust wants to be able to do this.
The code included
below demonstrates that a class extending Observable
can be created. The problem arises when one tries
to implement the clone method. The internal list
of Observers maintained by the Observable cannot
be copied in this implementation. Thus the cloned
Observable shares a list of Observers with its
original.
This is a little test program that exhibits the problem with cloning Observables.
public class ObservableBug
{
public static class MyObservable
extends Observable
implements Cloneable
{
public MyObservable myClone()
{
try{
return (MyObservable)clone();
}
catch(CloneNotSupportedException ex){
return null;
}
}
}
public static void main(String[] args) {
//
// Create an Observable to test.
//
MyObservable observable = new MyObservable(); //
// Give it an Observer.
//
observable.addObserver(
new Observer(){ public void update(Observable o, Object args){} } );
System.out.println(
"Created Observable, it has "
+ observable.countObservers() + " Observers.");
//
// Make a copy using clone.
//
MyObservable copy = observable.myClone();
System.out.println(
"Created copy, it has " + copy.countObservers() + " Observers.");
//
// Clear the copy's observers.
//
copy.deleteObservers();
System.out.println(
"Called deleteObservers on copy, it now has " + copy.countObservers() +
" Observers.");
System.out.println(
"Original Observable now has "
+ observable.countObservers() + " Observers.");
}
}
"How can we clone an Observable?"
Problem is that Observable doesn't implement it's own Clone() method;
Cust wants to be able to do this.
The code included
below demonstrates that a class extending Observable
can be created. The problem arises when one tries
to implement the clone method. The internal list
of Observers maintained by the Observable cannot
be copied in this implementation. Thus the cloned
Observable shares a list of Observers with its
original.
This is a little test program that exhibits the problem with cloning Observables.
public class ObservableBug
{
public static class MyObservable
extends Observable
implements Cloneable
{
public MyObservable myClone()
{
try{
return (MyObservable)clone();
}
catch(CloneNotSupportedException ex){
return null;
}
}
}
public static void main(String[] args) {
//
// Create an Observable to test.
//
MyObservable observable = new MyObservable(); //
// Give it an Observer.
//
observable.addObserver(
new Observer(){ public void update(Observable o, Object args){} } );
System.out.println(
"Created Observable, it has "
+ observable.countObservers() + " Observers.");
//
// Make a copy using clone.
//
MyObservable copy = observable.myClone();
System.out.println(
"Created copy, it has " + copy.countObservers() + " Observers.");
//
// Clear the copy's observers.
//
copy.deleteObservers();
System.out.println(
"Called deleteObservers on copy, it now has " + copy.countObservers() +
" Observers.");
System.out.println(
"Original Observable now has "
+ observable.countObservers() + " Observers.");
}
}