When to use delegates

J

jon-boy

Hi there,

More of a general question, rather than an issue.

Basically, I'm wondering where and when I should be using delegates in
my applications? I obviously use events, so do use them, but I've
never once declared my own delegates in code.

So, am I missing out on something good? I understand them in theory,
but every example I see on the internet is trivial and stupid - I just
can't relate these to real-world business problems that I face and
seemingly manage to solve without them.

Can anyone give a good run down of them and any potential areas in my
designs that I should look to use them?

Thanks,
Jon-Boy
 
A

Alexander Bartolich

jon-boy said:
[...]
Basically, I'm wondering where and when I should be using delegates in
my applications? I obviously use events, so do use them, but I've
never once declared my own delegates in code.

To my knowledge the term "delegate" is
- a generic design pattern of object-oriented programming
- short for "Business Delegate", a special pattern in Java EE
- an element of the Objective-C GUI framework of NeXTStep, OpenStep,
and MacOS X
- an element of the C# GUI framework of Windows and Mono

It has no defined meaning for the language C, though.
 
B

BGB / cr88192

jon-boy said:
Hi there,

More of a general question, rather than an issue.

Basically, I'm wondering where and when I should be using delegates in
my applications? I obviously use events, so do use them, but I've
never once declared my own delegates in code.

So, am I missing out on something good? I understand them in theory,
but every example I see on the internet is trivial and stupid - I just
can't relate these to real-world business problems that I face and
seemingly manage to solve without them.

Can anyone give a good run down of them and any potential areas in my
designs that I should look to use them?

if you have gone and confused C and C#, then this is probably worthy of
scorn...
after all, C# is probably 2x-3x more unrelated to C than C++ is to C...
but, hell, at least, probably, this was not a question about Java or
JavaScript...


but, no, the closest we have here in C land is:
typedef int (*Foo)(int x, int y);
and similar...

and as for usage:
rarely...

reason:
most usages involving function pointers don't tend to involve large scale
re-use of function pointers, and as well, this does not help with actually
declaring the function...

Foo bar
{
...
}

would be interesting, but I don't think this is syntactically valid...

however, OpenGL uses a lot of them:
PFNGLATTACHSHADERPROC pglAttachShader;
PFNGLBINDATTRIBLOCATIONPROC pglBindAttribLocation;
PFNGLCOMPILESHADERPROC pglCompileShader;
PFNGLCREATEPROGRAMPROC pglCreateProgram;
PFNGLCREATESHADERPROC pglCreateShader;
PFNGLDELETEPROGRAMPROC pglDeleteProgram;
PFNGLDELETESHADERPROC pglDeleteShader;
PFNGLDETACHSHADERPROC pglDetachShader;

and, elsewhere:
pglAttachShader=wglGetProcAddress("glAttachShader");
pglBindAttribLocation=wglGetProcAddress("glBindAttribLocation");
pglCompileShader=wglGetProcAddress("glCompileShader");
pglCreateProgram=wglGetProcAddress("glCreateProgram");
pglCreateShader=wglGetProcAddress("glCreateShader");
pglDeleteProgram=wglGetProcAddress("glDeleteProgram");
pglDeleteShader=wglGetProcAddress("glDeleteShader");
pglDetachShader=wglGetProcAddress("glDetachShader");


now, see how related all this is?...
 
P

Phil Carmody

jon-boy said:
Hi there,

More of a general question, rather than an issue.

Basically, I'm wondering where and when I should be using delegates in
my applications? I obviously use events, so do use them, but I've
never once declared my own delegates in code.

Design patterns are a large component in the dumbing down of
modern software engineering. It's almost as if they were invented
so that the programmer doesn't need to think. You end up with
O(lg(n)) problems being "solved" with O(n^2) implemetations.

That, with n in the thousands, was a nightmare I one saw in a
r-t/e context a few years back. I was reminded of that just this
morning, as I heard that it's a cancer that plagues some bits of
our current (again, r-t/e) project too. Just another reason DBus
sucks, I guess, not that I needed any more evidence of that.
So, am I missing out on something good? I understand them in theory,
but every example I see on the internet is trivial and stupid - I just
can't relate these to real-world business problems that I face and
seemingly manage to solve without them.

Can anyone give a good run down of them and any potential areas in my
designs that I should look to use them?

Never mutilate your approach to a problem to fit a solution to
a different problem that you already have.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top