Delegates in JAVA with MethodHandle

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值