java.util.Observable but with generics to avoid casts This is an Observable class similar to java.util.Observable. The difference is that it uses generics to avoid casts. The question is: Is it really worth the effort? What are the problems with this code?

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&lt;T&gt;  {
   private boolean changed = false;

   // Holds registered observers.
   // @TODO if class is not final can we make this protected? (should we?).
   private final Collection&lt;Observer&lt;? super T&gt;&gt; observers;

   /**
    * observersSet gives the opportunity of changing the implementing set. The
    * benefit is, final user can specify the 'order' of observers notified.
    * Generally, not a good idea at all.
    *
    * @TODO should this be private and the class final?.
    */
   protected Observable(Collection&lt;Observer&lt;? super T&gt;&gt; observersSet) {
      this.observers = observersSet; }

   public static &lt;F&gt; Observable&lt;F&gt;getInstance() {
      // return getInstance(container);
      ArrayList&lt;Observer&lt;? super F&gt;&gt; container = new ArrayList&lt;&gt;();
      return new Observable&lt;&gt;(container);
   }

   /**
    * Before this method can be actually used, this.update(T arg) must be fixed.
    * It has a hard-coded ArrayList. Generally, not a good idea, not at all.
    *
    * User would expect the call getInstance(ArrayList&lt;SomeObserver&gt; ar) to
    * work however it wont. It must be:
    * getInstance(ArrayList&lt;? extends SomeObserver&gt; ar);
    *
    * It's bad, ugly, and more importantly, violating.
    *
    * Unless of course the first example could be made to work. Maybe a cast
    * will do.
    */
   @SuppressWarnings("UnusedParameters")
   public static &lt;F&gt; Observable&lt;F&gt;
      getInstance(Collection&lt;Observer&lt;? super F&gt;&gt; observersSet) {
      throw new UnsupportedOperationException("Not yet implemented.");
      // return new Observable&lt;&gt;(observersSet);
   }

   // @TODO null check maybe for add?
   public synchronized boolean add(final Observer&lt;? super T&gt; o) {
      return !this.observers.contains(o) &amp;&amp; observers.add(o);

   }
   public void addObserver(final Observer&lt;? super T&gt; o)         { this.add(o); }

   public synchronized boolean rm(final Observer&lt;? super T&gt; o)  { return observers.remove(o); }
   public void deleteObserver(final Observer&lt;? super T&gt; o)      { this.rm(o); }

   public synchronized void clear() { this.observers.clear(); }
   public void deleteObservers()    { this.clear(); }
   // @TODO all of them need synchronized?
   public synchronized void setFlag() { this.changed = true; }
   public synchronized void clrFlag() { this.changed = false; }

   // Hei! did you know if you used projectLombok.org you could remove this
   // method? didn't you?!!! :O 0_o
   public synchronized boolean getFlag() { return this.changed; }
   public boolean hasChanged() { return this.getFlag(); }
   public int countObservers() { return this.observers.size(); }

   public void update(final T args) {
      ArrayList&lt;Observer&lt;? super T&gt;&gt; copy;

      synchronized (this) {
     if(!changed) return;
     copy = new ArrayList&lt;&gt;(this.observers);
     this.clrFlag();
      }
      // Look at OpenJDK to see why a new Collection is used.
      for(Observer&lt;? super T&gt; o : copy) o.update(args);
   }
   public void notifyObservers(final T args) { this.update(args); }

   public void update() { this.update(null); }
   public void notifyObservers() { this.update(); }
}


Observable now carries type of args sent to each Observer, and Observer is also aware of it, thus no cast needed.



The follow up question is located here.
Pick a convention for your curly brackets/indentation and stick with it. You code has all of the following:


Brackets included, one-lined statement
Brackets omitted, one-lined statement
New line after opening bracket, closing bracket on same line.
New line after opening bracket, closing bracket on different line.
Un-indented code inside brackets.
Padding spaces to vertically aligning opening brackets (with multiple different vertical lines being padded to).
Single space padding opening brackets.
Blank line before closing bracket.


The Java convention is:

public synchronized boolean add(final Observer&lt;? super T&gt; o) {
   return !this.observers.contains(o) &amp;&amp; observers.add(o);
}


It is best to always include curly brackets even when they could be omitted. If you start with an if that does not include brackets and then need to add a second line, forgetting to would have the second line execute unconditionally.



It is safer to synchronize on a private instance variable than on the class instance. You have no control over what someone does with the instance of your class. Maybe they think it would be a good idea to use it to synchronize some other code. Now you have two different pieces of code using the same lock instance that might be result in a deadlock because they are not coordinated properly. A private lock variable guarantees that this can't happen.



You have to synchronize all accesses to observers. countObservers() could be called while you are adding or removing an observer.



Don't create aliases for your methods. It will only lead to confusion if there are multiple ways to do the same things.



observersSet is not a Set Don't make the argument name appear as if it is expecting one. If you do want it to be a set (you might since you are checking for duplicates in add()), then change the type from Collection to Set. If you need observers to be called in the order in which they were added, LinkedHashSet satisfies this requirement. If you accept any Collection when creating the instance, there is nothing to prevent the collection from already containing a duplicate.