Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tonight at JGO: Java vs C# vs C++ vs X  (Read 1784 times)
0 Members and 1 Guest are viewing this topic.
Offline zero

Junior Duke





« Posted 2006-03-19 21:22:53 »

[size=12pt]1. Round: function pointers vs. delegates vs. anonymous classes[/size]

C++ functions are knocked out by the first hit, since class methods can't be ones, and they are unsafe ;-)

C# delegates seem to have good foot work, they are easy to use,

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
// definition
delegate void Foo(object arg);

// implementation
Object anObject = ..;

Foo foo = delegate(object arg) {
    Console.WriteLine(arg.ToString() + anObject);
};

// calling
foo("Hello");


but there comes Java, leaves its defense and hits back with it's anonymous classes, which are even more powerfull , beeing object oriented by encapsulating data:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
// definition
interface Delegates {
 void foo(Object arg);
 void bar(); // yeah not only one function
}

// implementation
final Object anObject = ..;

Delegates delegates = new Delegates() {
  public void foo(Object arg) {
      System.out.printLine(arg.ToString() + anObject);
  }
   public void bar() {
      System.out.printLine("Bar");
   }
};

// calling
delegates.foo("Hello");
delegates.bar();


so far a slightly advantage for Java. But wait, there comes X - the underdog, which is exactly like using Java's anomymous methods, but has a nice syntax sugar for interfaces with only a single method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
// definition
interface Delegate {
 void foo(Object arg);
}

// implementation
final Object anObject = ..;

Delegates delegate = new Delegate.foo(Object arg) { // the single method, right behind the interface name
      System.out.printLine(arg.ToString() + anObject);
};

// calling
delegate.foo("Hello");


nice, isn't it Wink


to be continued  Tongue
Offline woogley
« Reply #1 - Posted 2006-03-19 22:06:35 »

one advantage delegates have though is executing several methods in one call.

e.g.:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
delegate void Foo(string s);
class Test {
   public Test() {
      Foo f = null;
      f += new Foo(method1);
      f += new Foo(method2);
      f += new Foo(method3);
      f += new Foo(method4);
      f("bar"); // executes all 4 methods
      f -= new Foo(method3);
      f("bar"); // executes all methods except method3
   }
   protected void method1(string s) { /* whatever */ }
   protected void method2(string s) { /* whatever */ }
   protected void method3(string s) { /* whatever */ }
   protected void method4(string s) { /* whatever */ }
   static void Main(string[] args) { new Test(); }
}

pretty handy in some situations.. oO;

p.s. I'm just playing devil's advocate here. I've been doing Java for 8 years but I gotta admit I really enjoy C# also Smiley

edit: oops, forgot you cant captilize "String" in C#  Roll Eyes
Offline zero

Junior Duke





« Reply #2 - Posted 2006-03-20 04:54:59 »

It's some time ago, but I've programmed c# for two years and I'm very sure you can't do this! You'll need an event-object, which can collect delegates of the same type, s.th.

1  
2  
3  
4  
5  
delegate void Foo(string s);

public event Foo FooEvent;

FooEvent += new Foo(method);


Do you know what the default implementation of an event-object is? It's similiar like 'Set' of Java's EvenListeners. Of course '+=' is shorter in syntax than addXXXListener, but as you know in Java a listener interface can have more methods, so one addWindowListener is equivalent to more c# events (Form's Close, Closing, ..) and if you want to override only a single method, use the  WidnowAdapter class.

but thanls for the hint, I'm concerning 'Event Handling' as another round in this bare-knuckle fight  Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline woogley
« Reply #3 - Posted 2006-03-20 04:58:54 »

It's some time ago, but I've programmed c# for two years and I'm very sure you can't do this!

I program C# reguarly, you can indeed do this Wink the process I showed you is not limited to 'event' types, it is the main purpose of delegates.

go ahead, run this code: (.NET 2.0)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
using System;
delegate void Foo(string s);
class Test {
   static void Main(string[] args) {
      new Test();
   }
   public Test() {
      Foo f = null;
      f += new Foo(method1);
      f += new Foo(method2);
      f += new Foo(method3);
      f += new Foo(method4);
      f("bar"); // executes all 4 methods
      Console.WriteLine("");
      Console.WriteLine("removing method3 ...");
      Console.WriteLine("");
      f -= new Foo(method3);
      f("bar"); // executes all methods except method3
      Console.ReadKey();
   }
   protected void method1(string s) {
      Console.WriteLine("method1: "+s);
   }
   protected void method2(string s) {
      Console.WriteLine("method2: "+s);
   }
   protected void method3(string s) {
      Console.WriteLine("method3: "+s);
   }
   protected void method4(string s) {
      Console.WriteLine("method4: "+s);
   }
}


output:
Quote
method1: bar
method2: bar
method3: bar
method4: bar

removing method3 ...

method1: bar
method2: bar
method4: bar
Offline zero

Junior Duke





« Reply #4 - Posted 2006-03-20 05:07:26 »

woaaah, guess this was kind of a shadow kick ;-)

didn't know that, altough I've worked with c# 2.0 beta most the times.. since i'm currently on linux, i cannot test your code but i believe you that it runs well.. btw. can you tell me how to overload the +=/-= operators for a delegate, I have in mind how to do this for an event object but for delegates no idea - thanks Smiley
Oh, and another point, is how the default implementation looks, are the delegates stored in a list like for events?
Offline woogley
« Reply #5 - Posted 2006-03-20 05:17:57 »

I dont believe you can overload those operators on a delegate since it's considered a primitive (such as int and byte etc). the only way you might can do it is if the return type of delegate overloads it. e.g:
1  
delegate Something Foo(String s);

if the Something class overloads the += operator, that might be enough to do it. *shrug* I will look into it. sort of pointless in this topic since Java offers no operator overloading Smiley

I would suppose the delegates use a list of some sort since you can add/remove methods to one delegate. By the way, you can add any method to a delegate provided the method shares the same signature as the delegate (e.g. returntype and parameters).

Events in C# seem to be just specialized delegates. But with further experience I may prove myself wrong Smiley

edit:
one advantage event-wise in C# is that 99% of the time you dont even have to worry about event listeners (er, well, "callbacks" would be a more precise term). For example, the System.Windows.Forms class pre-defines every method for every event that could possibly happen to that window. there's no need to add any listeners, just override an event method that you need to use Wink
Offline zero

Junior Duke





« Reply #6 - Posted 2006-03-20 05:38:32 »

I was just to curious, so I did some research:

Combining delegates, create an instance of System.MulticastDelegate, which has a linked list of delegates, called an invocation list, consisting of one or more elements. Actually not much difference of an event I guess. well on the one hand its nice syntax sugar again. on the other however, having a null initialized object at a given line and using it in the next one on the right side of the expression (a += b implies  a = a+ b) really makes my eyes bleed:

1  
2  
3  
4  
..
Foo f = null;
f += new Foo(method1);
..
Offline woogley
« Reply #7 - Posted 2006-03-20 05:56:16 »

ah, that's only because it's fairly new syntax Wink

null+something isn't really a bad thing. just like 0+1 isnt 0 Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!