title: Java - Generic ChangeListener
body: <p>Scenario:</p>
  
  <p>I have a container object which holds a mixed bag of objects all inheriting from the MyContainedObject class. Consumers of the container class do not have direct access to contained objects, but my be interested in knowing when they change. </p>
  
  <p>Design Decision:</p>
  
  <p>What's the best way to listen for ChangeEvents on a specific class type? My initial thought is to do something with Generics. For example,</p>
  
  <pre><code>private TreeMap&lt;Class, ChangeListener&gt; listeners;
  
  public &lt;T extends MyContainedObject&gt; addChangeListenerForObjectsOfType(Class&lt;T&gt; className, ChangeListener listener)
  {
     listeners.put(className, listener);
  }
  </code></pre>
  
  <p>When a change is detected the container class would iterate through the list and notify only listeners registered for that class type.</p>
  
  <p>Other suggestions?</p>
  
  <p>Thanks.</p>