• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Callback references

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
How do I pass a callback to a routine, and then invoke it? What I have in mind is something like the code below.
TIA,
Richard
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're an ex-C/C++ programmer, right?
Rather than passing pointers to methods around, you should investigate the use of interfaces.
You define an interface that defines the method signature (or signatures) that you need, and what you then pass is a reference to an instance of an object that implements that interface.
hth,
bear
 
Richard Muller
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bear,

You're an ex-C/C++ programmer


You've got that right!
Thanks for pointer [pun intended] in the right [Java] development.
Regards,
Richard
 
Richard Muller
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked at interfaces a little, but didn't see how to apply them in this situation.
I finally got something reasonable working using an auxilliary class. Can we do better with interfaces (or any other way)?

[ May 07, 2002: Message edited by: Richard Muller ]
[ May 07, 2002: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some of your code is a bit redundant. Here is just a quick revision that keeps the spirit of the old code.

[ May 07, 2002: Message edited by: David Weitzman ]
 
Richard Muller
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
Thanks for commenting on my code. I've revised the code for and packaged Status, as shown below.
I continued passing the st argument because I need to reinitialize the static st object on every use of Status -- I'm confident you concur - and IMHO it's nicer to do it with the invocation of a routine rather than having it as a (forgettable) stand-alone line.
I did eliminate the unnecessary initializations in the constructor. In fact, I eliminated the explicit constructor entirely.
Below is the revised Status and an example of its utilization.
Again, thanks for your suggestions.
I'm still interested whether use of an interface would reduce the code in any respect.
Regards,
Richard
Status.java

DemoStuff.java

[ May 07, 2002: Message edited by: Richard Muller ]
[ May 07, 2002: Message edited by: Richard Muller ]
[ May 07, 2002: Message edited by: Dirk Schreckmann ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Those crazy stubborn C++ people. You can't take them anywhere. At the very least I see you've used one aspect of java naming conventions (ReadConsoleLine vs Read_Console_Line). You've still got a redundant 'extends Object' though, which I ought to point out.
Just for the record, I most certainly don't concur. I'll try not to force OO practices or java standard coding techniques down your throat, though (you'll learn them eventually).
To give you an idea of what the program you've developed would normally look like, I'll rewrite it:

[ May 07, 2002: Message edited by: David Weitzman ]
 
Richard Muller
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
Thank you for continuing offer guidance.
I agree with you that your code for reading a line is obviously far better than mine ... if the goal were simply "read a line from the console".
But I'm trying to fry bigger fish.
(1) I want to simply the use of java for myself and perhaps other C++ programmers. So writing

instead of

is preferable to me.
Actually, I want a little more than that: I want minimal effort to detect and respond to errors.
(2) I'm not interested in doing this merely for one simple statement. The code below demonstates the several simplifications (to my mind) that I coded.
Pursuant to your suggestions, I improved:
(1) Removed extends Object: I stuck it in because I got a diagnostic regarding my defining toString(). Thanks to you, I now see that that "solution" was incorrect. It now occurs to me that every class inherently extends Object, so I see why it�s redundant.
(2) Removed st arguments; included initialization in all the support routines.
Following is the full code I have. In the light of all the above, I hope you take a more kindly view of it, but I would still value any guidance you may wish to provide. Again, thanks for all the time you�ve contributed to this little idea.
DemoStuff


Status

[ May 08, 2002: Message edited by: Richard Muller ]
[ *** TAB characters removed *** ]
[ May 09, 2002: Message edited by: Dirk Schreckmann ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your newer handling of the Status seems much nicer. It doesn't really need its own package though (unless you're planning to reuse it in other projects). Usually a package holds several classes that combine to provide a reusable functionality. The line, bError? sMessage : "", is also still unnecessary.
I suppose writing C++ (or rather C, since you aren't using any object-oriented features) in java is as good a place to start as any, although using a special status object to handle errors still seems pretty odd. Maybe future version will do more than simply print the error message any countinue.
Just for amusment value you could replace all calls to System.out.println() with calls you a function of your own, cout().
You might also consider calling RunExecutable() like this: RunExecutable(new String[] {"program.exe", "arg1", "arg2"})
This eliminates the need to create all those variables (as1, as2, ..., as12, as13) every time you want to call it. If that doesn't do it for you, you could at least just reuse one variable ('as') over and over.
 
Richard Muller
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Status � doesn't really need its own package though (unless �
I am definitely planning reuse.
> The line, bError? sMessage : "", is also still unnecessary.

Woops, great observation!
> � aren't using any object-oriented features
As Stroustrup said, he sees C++ as a �better C�, so I think of what I write as C++ all the time, even without coding polymorphism, etc. :-)
> � using a special status object to handle errors still seems pretty odd.
Granted, it probably looks weird to seasoned Java programmers, but to me it compensates for my loss of a preprocessor that lets me write my own shorthand. Old habits die hard. :-)
> � replace all calls to System.out.println() with calls to your own cout()
You�re prescient: you�re reading my mind before I even had the idea. Writing that long string has been bugging me! Now, I�ve just got to decide on the syntax to distinguish between:

Maybe cout/coutnl, though two extra letters my kill me. :-)
> RunExecutable(new String[] {"program.exe", "arg1", "arg2"})
That�s great syntax! Exactly the style I�m trying to adopt.
Finally, thanks again for all you help in educating me in Java. I�m sure our paths will cross again as I post other questions.
Regards,
Richard
P.S.
1.Obviously my post of the code omitted one thing: the subdirectory containing the (C++ based) binary executable that the RunExecutable invoked. I should probably have mentioned that.
2.All the routines other than main and Status were based on published code from TechRepublic's Java Anthology
[ May 09, 2002: Message edited by: Richard Muller ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic