Y
yancheng.cheok
Hi all
I wish to have to a generics version of observer/ observable pattern?
just like the one which is implemented in c++ template.
http://www.codeproject.com/cpp/observer_with_templates.asp
I tried the following implementation. However, I get the following
compilation error. May I know what wrongs?
C:\Documents and Settings\yccheok\Desktop\java\observer\src\jstock
\Subject.java:31: update(jstock.Subject<jstock.Observer<T>,T>,T) in
jstock.Observer<T> cannot be applied to
(jstock.Subject<TObserver,T>,T)
package jstock;
/**
*
* @author yccheok
*/
public class Subject<TObserver extends Observer<T>, T> {
/** Creates a new instance of Subject */
public Subject() {
observers = new java.util.concurrent.CopyOnWriteArrayList<
TObserver >();
}
public void attach (TObserver observer)
{
observers.add(observer);
}
void _notify (T arg)
{
for(java.util.Iterator< TObserver > iterator =
observers.iterator(); iterator.hasNext(); ) {
iterator.next().update(this, arg);
}
}
private java.util.List< TObserver > observers;
}
package jstock;
/**
*
* @author yccheok
*/
public interface Observer<T> {
public void update(Subject< Observer<T>, T > subject, T arg);
}
I wish to have to a generics version of observer/ observable pattern?
just like the one which is implemented in c++ template.
http://www.codeproject.com/cpp/observer_with_templates.asp
I tried the following implementation. However, I get the following
compilation error. May I know what wrongs?
C:\Documents and Settings\yccheok\Desktop\java\observer\src\jstock
\Subject.java:31: update(jstock.Subject<jstock.Observer<T>,T>,T) in
jstock.Observer<T> cannot be applied to
(jstock.Subject<TObserver,T>,T)
package jstock;
/**
*
* @author yccheok
*/
public class Subject<TObserver extends Observer<T>, T> {
/** Creates a new instance of Subject */
public Subject() {
observers = new java.util.concurrent.CopyOnWriteArrayList<
TObserver >();
}
public void attach (TObserver observer)
{
observers.add(observer);
}
void _notify (T arg)
{
for(java.util.Iterator< TObserver > iterator =
observers.iterator(); iterator.hasNext(); ) {
iterator.next().update(this, arg);
}
}
private java.util.List< TObserver > observers;
}
package jstock;
/**
*
* @author yccheok
*/
public interface Observer<T> {
public void update(Subject< Observer<T>, T > subject, T arg);
}