Trywialnym rozwiązaniem byłoby:
interface Delegate<T> {
public void call(T value);
}
class DelegateList<T> {
private List<Delegate<T>> delegateList = new ArrayList<Delegate<T>>();
public boolean addDelegate(Delegate<T> delegate) {
return delegateList.add(delegate);
}
public void callDelegates(T value) {
for (Delegate<T> delegate : delegateList) {
delegate.call(value);
}
}
}
Tylko, że teraz trzeba tworzyć explicite klasy anonimowe, które delegują.
Scala jest dużo lepsza jeżeli chodzi o delegaty, domknięcia, itp itd Przykładowy kod wygląda tak:
class DelegatesList[A] {
private val list = new scala.collection.mutable.ListBuffer[A => Unit]()
def add(a: A => Unit*): Unit = list.append(a: _*)
def call(a: A): Unit = list.foreach(_(a))
}
object Main {
def fun1(x: Int) = println(x + 5)
def fun2(x: Int) = println(x + 8)
def fun3(x: Int) = println(x * 7)
def main(args: Array[String]) {
val list = new DelegatesList[Int]
list add(fun1, fun2, fun3)
list call 5
}
}
Polecam Scalę, C# się chowa przy niej :)
PS:
Byłbym zapomniał. Scala bardzo dobrze integruje się z Javą. Prawie zawsze Scala może dziedziczyć po Javowych klasach (pewnie są jakieś rzadkie wyjątki, mam na myśli enumy, ale po enumach i tak się nie dziedziczy), a często da się także w drugą stronę (o ile zastosuje się do odpowiednich reguł).