Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Event in a class

Thread view: 
ffellico@inwind.it - 19 May 2006 17:48 GMT
Hi.

I would like to create a Java class (named for example fileInp) where
it's possible from a program that use it to activate during processing
of a method of fileIn an execution of a method of the program that have
create a fileIn object.

To explain better my need, suppose that I have a class named fileInp
whith a method named Execute; the Execute method is a loop that read
all the record of a ascii file; I would like to insert in the loop an
execution of the following method:

                  public void myProcessing(String record)

wichi is in my program, where record is the line ascii just readed, so
my program can elaborate it and then passing back the control to the
Excecute method for the next reading.

The name myProcessing must be not fixed but I need to comunicate it to
the class fileInp eventually through another fileInp method; also in
same case I need to comunicate to the fileInp class that I desire to
nullify the execution of my method.

I realized this kind of processing in Delphi without problems, but in
Java I am a beginner so I need some help to understand how do it.

Thank you from Franco in Italy.
Oliver Wong - 19 May 2006 19:02 GMT
> Hi.
>
[quoted text clipped - 21 lines]
> I realized this kind of processing in Delphi without problems, but in
> Java I am a beginner so I need some help to understand how do it.

   The traditional way is to in fact, fix the name of the method to
something (e.g. "myProcessing") by creating an interface that your
processing class has to implement.

   If you really need to get rid of this fixed-name requirement, you could
perhaps use reflection to pass in a reference to the method itself, and have
your fileInp class execute that method.

   - Oliver
ffellico@inwind.it - 20 May 2006 05:38 GMT
Oliver Wong ha scritto:

> > Hi.
> >
[quoted text clipped - 31 lines]
>
>     - Oliver
ffellico@inwind.it - 20 May 2006 05:59 GMT
Oliver Wong ha scritto:

> > Hi.
> >
[quoted text clipped - 31 lines]
>
>     - Oliver

Thank you Oliver.
Your suggestions will sure help me in finding the solution. Any way I
have a lot of doubt:

1.
If I fix the name e. g. to myProcessing, how I can buid the class
(without errors) since it have a reference to myProcessing method and
myProcessing is not yet available?

2.
Could be a good idea reference instead a dummy method inside fileInp
(wich do nothing) and in some way change it's reference dinamically to
point to the method of my processing class?

So, some other comment could be appreciated. Thanks again. Franco.
Andy Flowers - 20 May 2006 06:35 GMT
> Oliver Wong ha scritto:
>
>>> Hi.

> 1.
> If I fix the name e. g. to myProcessing, how I can buid the class
> (without errors) since it have a reference to myProcessing method and
> myProcessing is not yet available?

The processing code interface you define will provide this name for you.

i.e.

public interface MyProcessorInterface
{
  public void myProcessing(String record);
}

The you can create concrete classes that implement this interface

i.e.

public class ImplementIt1 implements MyProcessorInterface
{
  public void myProcessing(String record);
  {
     // do my specific logic here
  }
}

public class ImplementIt2 implements MyProcessorInterface
{
  public void myProcessing(String record);
  {
     // do my different logic here
  }
}

Finally you pass an instance of a class that implements the interface to your
fileInp class.

i.e.

public class fileInp
{
  MyProcessorInterface processor = null;

  public fileInp(MyProcessorInterface processor)
  {
    this.processor = processor;
  }

  public void Execute()
  {
    while(!eof())
    {
      String record = readRecord();
      processor.myProcessing(record);
    }
  }
}

> 2.
> Could be a good idea reference instead a dummy method inside fileInp
> (wich do nothing) and in some way change it's reference dinamically to
> point to the method of my processing class?

> So, some other comment could be appreciated. Thanks again. Franco.
Dražen Gemić - 20 May 2006 16:07 GMT
If it needs to be an Event, you can look at java.util.EventObject
and java.util.EventListener. Keep in mind that the class that posts
an event will not wait for event handler to return. Not by itself.

DG
Andy Flowers - 20 May 2006 20:52 GMT
> If it needs to be an Event, you can look at java.util.EventObject
> and java.util.EventListener. Keep in mind that the class that posts
> an event will not wait for event handler to return. Not by itself.
>
> DG
or Observer & Observable.

If I understand the OP he has used Delphi and this has an easy way to replace
method addresses at run time, it basically uses pointers behind the scenes. This
is one of the techniques that made it into C# (same language designer) using, if
I recall, delegate & event.
ffellico@inwind.it - 21 May 2006 08:12 GMT
Andy Flowers ha scritto:

> > Oliver Wong ha scritto:
> >
[quoted text clipped - 64 lines]
>
> > So, some other comment could be appreciated. Thanks again. Franco.

Thank you Andy.

I have worked a lot with my problem and now I solved it using
reflection as Oliver suggest me.  I learned a lot of things working
around this problem!.

Now I will work with interfaces as you suggest and I will refine my
solution, but  mainly all this work will be a useful job to improve my
know-how in this powerful language.

So many thanks to all of you. Franco
Rhino - 19 May 2006 19:10 GMT
> Hi.
>
[quoted text clipped - 23 lines]
>
> Thank you from Franco in Italy.

If I understand your situation correctly, you may find Runtime.exec()
useful. You could pass it each command and have that command executed for
you. This article explains how to use Runtime.exec():

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html

--
Rhino


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.