Recently, I try to rewrite a PDA client communication software, which original is in C#, in JAVA. It's very convenient as using Delegates in C#. As long time no using JAVA and not familary with it. I seacher it in Internet and ask some firends. But the answer is "No directly surpports".
From the net, I use InnerClass and interfaces to complete it. I define interface "IDelegatehandler" as the root super class for my "delegates"."IDelegatehandler" has only one function definition "public void onHandlPerform(Object sender)".
Some say to use reflect butit seems...
For each event's handling, I code a super Interface class to define the basic events handle interface. In my concept, Interfaces are using to define "Abilities" and it's usful as one class can implement many interfaces.
Such as, For ConnectionStateChanged event:
1. IConnectionStateChangedHandler
public interface IConnectionStateChangedHandle extends IDelegatehandler{
public void onConnectionSateChanged(Object sender);
}
2.ConnectionStateChangedHandler
public class ConnectionStateChangedHandler implements IConnectionStateChangedHandle {
public void onConnectionSateChanged(Object sender){}
public void onHandlPerform(Object sender){}
}
Also it's OK. But it takes longs works and codes.
Today, I view "Java Plateform SE 7"'s doc. Suddently, there is a package named "java.lang.invoke". The key word "invoke" makes my eyes sparking. Open it, Classes:
MethodHandle, MethodHandles,...
From them, it seems that a new clean, pretty and blue sky comes to my eyes.
References:
1.Java vs. C# delegate(Reflect).http://www.javacamp.org/javavscsharp/delegate.html;
2.http://stackoverflow.com/questions/44912/java-delegates;
3.http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html;
4.通过代码简单介绍JDK 7的MethodHandle,并与.NET的委托对比.http://rednaxelafx.iteye.com/blog/477934.
5.Java7 MethodHandle 例子(1).http://www.oschina.net/code/snippet_116768_7627.