Events Handling in Java and C#
Actually, this article is posted about one year before, at here. Today, I happened to see this article, interesting. So, I found my old entry and post here. Also, I post all the comments at the end, it’s really valuable I think.
Recently(2004.08.06), a friend of mine discussed the Event handle issues in C# and Java with me, sounds interesting. I would like to blog the discussion and my investigation here.
C# (pronounced C Sharp) is a programming language developed by MS as part of its .NET platform. When I saw it the first time, I ask myseft:”is it another version of Java?”. Just a joke, C# is extremely similar to Java: GC, VM, single inheritance, interfaces, packages(use “using” instead of “import” :P), pure OO etc. However, C# also invent many new features, delegate is a famous one.
Ok, let’s talk about the event-handle issues. In Java, we use the well-known Observer Pattern (Gamma et, Design Patterns) to implmented the events. For example, if we wanna to handle the Button’s Click event, we simply add a ActionListener to this button:
- JButton button = new JButton("ok");
- button.setActionCommand("ok");
- button.addActionListener(new Handler());
- ......
- class Handler implements ActionListener {
- public void actionPerformed(ActionEvent ev) {
- String command = ev.getActionCommand();
- if (command == "OK") {
- onOK();
- }
- ......
- }
- }
Or we can even add listener in following way:
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent ev) {
- String command = ev.getActionCommand();
- if (command == "OK") {
- onOK();
- }
- ......
- }
- });
In C#, Anders Hejlsberg introduced the new keyword “delegate”, I think it’s actually like the type-safe function pointers. Anders really love the function pointers, he bring this to Delphi & Visual J++ too. Let’s first take a look at how this keyword works:
In Java, Since the listener rely on the interfaces, so, we can not call the methods without knowledge of the target object. For example:
- public class Class1 {
- public void show(String s) {};
- }
- public class Class2 {
- public void show(String s) {};
- }
A more complex example is the method whose name can be variant, you can find detail information in this article.
Although these two classes have common method, but because they do not share a common interface, so, we can not call them in a uniform way. How to solve the problem? In Java, we can use the Proxy Pattern(Gamma et, Design Patterns) and Adapter Pattern(Gamma et, Design Patterns) or use the reflection mechanism to solve the problem. In C#, ok, it’s delegate’s work.
- public delegate void show(String s);
- show s1 = new show(new Class1().show);
- s1("Test");
- s1 = new show(new Class2().show);
- s1("Test");
The most important usage of delegates is for event handling:
- Button button = new Button();
- button.Click += new EventHandler(onOK); // BTW: we can see, method is the first-class object in C#.
- private void onOK(...) {
- ......
- }
Ok, that is the mechanism C# handle events. How about it? I think everyone has his cents. In my opnion, at the first look, it’s very attractive to me, things get easy now. But soon I feel uncomfortable. I don’t think this function pointer-like mechanism should exist in a pure OO languages :P. So, I would rather to use patterns to keep the code clean.
To summarize:
At the above situation:
1) If I can controll all the code, then I would rather retrofit a common interface that has “show” method;
2) If things are not that easy, for example: all classes are library codes, or method has different name, I’d like to use adapter and proxy pattern to solve this problems.
Just my cents.
Woty - not bad. delegate is some how usefule. but i think method should not be the first-class object. In common OO theory: Class := data + operation on data. if member operation is first-class, how about member varible?
Mijia - I think the delegate is just the wrapper for the function pointer or callback function in C/C++, which cannot be appeared in Java or C#, because they have their restrict security architecture. As well as first class object discussed, I do not know the exact ly defined concept, so I could not tell which idea would be correct. But I would like to regard it like that callback function or function pointer seems very dangerous in C/C++, but it is a fantastic mechanism which could provide flexibility for the software design. And I think Anders Hejlsberg from the C# loved it the most, so he cannot resist to add such mechanism into C#. But in Java, for the consideration of security, the function pointer is strictly forbidden, but Java could use the Reflect to emulate the behavior of the callback function or function pointers. And it is simple and flexible as well. So I think perhaps “function as first class” may be not pretty in the view of grammar or sementics, and in C# it is just one’s idea, some camouflage beneath the security flag.
Popularity: 33%
Related entries:
- No Related Posts
