Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This is the follow-up question from here here.

This is the follow-up question from here.

This is the follow-up question from here.

added 21 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Does javaJava need an Observable object with generics?

This is the follow-up question from here .

This is a Observablean Observable class similar to java.util.Observablejava.util.Observable. The difference is, that it uses generics to avoid casts.

There are two questions

  • Is it really worth the effort? or it's just making things more complex than needed?
  • Should this class be declared final? or it's ok to be subclassed? java.util.Observable is not final.

The code:

  1. Is it really worth the effort? or it's just making things more complex than needed?
  2. Should this class be declared final, or is it ok to be subclassed? java.util.Observable is not final.

Methods deleteObserver, deleteObservers, setChanged, clearChanged, hasChanged, countObservers, notifyObservers are removed for brevity.

Methods `deleteObserver`, `deleteObservers`, `setChanged`, `clearChanged`, ` hasChanged`, `countObservers`, `notifyObservers` are removed for brevity. *the rest of code at [GitHub](https://github.com/loolooyyyy/GenericObservable/blob/master/src/main/java/cc/koosha/silkroad/lang/Observable.java)*

The rest of the code is at GitHub .

Does java need an Observable object with generics?

This is the follow-up question from here

This is a Observable class similar to java.util.Observable. difference is, it uses generics to avoid casts. There are two questions

  • Is it really worth the effort? or it's just making things more complex than needed?
  • Should this class be declared final? or it's ok to be subclassed? java.util.Observable is not final.

The code:


Methods `deleteObserver`, `deleteObservers`, `setChanged`, `clearChanged`, ` hasChanged`, `countObservers`, `notifyObservers` are removed for brevity. *the rest of code at [GitHub](https://github.com/loolooyyyy/GenericObservable/blob/master/src/main/java/cc/koosha/silkroad/lang/Observable.java)*

Does Java need an Observable object with generics?

This is the follow-up question from here .

This is an Observable class similar to java.util.Observable. The difference is that it uses generics to avoid casts.

There are two questions:

  1. Is it really worth the effort? or it's just making things more complex than needed?
  2. Should this class be declared final, or is it ok to be subclassed? java.util.Observable is not final.

Methods deleteObserver, deleteObservers, setChanged, clearChanged, hasChanged, countObservers, notifyObservers are removed for brevity.

The rest of the code is at GitHub .

Source Link
hkoosha
  • 153
  • 1
  • 6

Does java need an Observable object with generics?

This is the follow-up question from here

This is a Observable class similar to java.util.Observable. difference is, it uses generics to avoid casts. There are two questions

  • Is it really worth the effort? or it's just making things more complex than needed?
  • Should this class be declared final? or it's ok to be subclassed? java.util.Observable is not final.

The code:

package cc.koosha.silkroad.lang; 
import java.util.ArrayList;
import java.util.Collection;
/**
 * like java.util.Observable, But uses generics to avoid need for a cast.
 * For any un-documented variable, parameter or method, see java.util.Observable
 */
public class Observable<T> {
 public interface Observer<T> {
 public void update(Observable observable, T args);
 }
 /**
 * Holds registered observers.
 * @TODO if class is not final can we make this protected? (should we?).
 */
 private final Collection<Observer<? super T>> registry;
 private boolean changed = false;
 /**
 * @TODO should this be private and the class final?.
 */
 protected Observable(final Collection<Observer<? super T>> registry) {
 this.registry = registry;
 }
 /**
 * Static constructor.
 */
 public static <F> Observable<F> getInstance() {
 ArrayList<Observer<? super F>> container = new ArrayList<Observer<? super F>>();
 return new Observable<F>(container);
 }
 // @TODO Should o be checked for null?
 public void addObserver(final Observer<? super T> o) {
 synchronized (this.registry) {
 if (!this.registry.contains(o))
 this.registry.add(o);
 }
 }
 
 protected void update(final T args) {
 ArrayList<Observer<? super T>> toNotify;
 synchronized (this.registry) {
 if (!changed)
 return;
 toNotify = new ArrayList<Observer<? super T>>(this.registry);
 this.changed = false;
 }
 // Look at OpenJDK to see why a new Collection is used.
 for (Observer<? super T> o : toNotify)
 o.update(this, args);
 }
 public void notifyObservers(final T args) {
 this.update(args);
 }

Methods `deleteObserver`, `deleteObservers`, `setChanged`, `clearChanged`, ` hasChanged`, `countObservers`, `notifyObservers` are removed for brevity. *the rest of code at [GitHub](https://github.com/loolooyyyy/GenericObservable/blob/master/src/main/java/cc/koosha/silkroad/lang/Observable.java)*
lang-java

AltStyle によって変換されたページ (->オリジナル) /