A creates-->B,C. B call-> A call-> C?
I hope I am missing something obvious here:
Say I have three objects A, B, and C. My program starts with object "A".
I use object "A" to create instances of objects "B" and "C". Object "B"
does some work and when "B" completes its work, I want it to raise an event
that object "A" can see so that object "A" can respond by making object "C"
do some work.
How can I enable object B to effectively call a method in object A (by raising
an event or something) so that object A can in turn call a method in object
C?
I've learned that object B can call a static method in object A. But the
rule for static methods is that they can only operate on the class to which
they belong (meaning that a static method in object A could not call a method
in object C).
You see, objects "B" and "C" do specialized work as all good objects should
do. But I need object "A" to be able to coordintate the activities between
"B" and "C".
How do I handle this?
Re: A creates-->B,C. B call-> A call-> C?
I hope you are too.
First of all, object B doesn't "do some work". But a method of object B
does do some work. So when object A calls that method, the method does the
work, whatever it is. After that, object A is free to do whatever it is
programmed to, such as calling a method in object C.
Here's a fragment of code that might be in object A:
B.doSomething(); // B does something
C.doAnotherThing(); // then C does something
It's really very simple. Don't try to make it complicated.
PC2
rneal <rneal@imseries.com> wrote in message news:3a357f71$1@news.devx.com...
>
> I hope I am missing something obvious here:
>
> Say I have three objects A, B, and C. My program starts with object "A".
> I use object "A" to create instances of objects "B" and "C". Object "B"
> does some work and when "B" completes its work, I want it to raise an
event
> that object "A" can see so that object "A" can respond by making object
"C"
> do some work.
>
> How can I enable object B to effectively call a method in object A (by
raising
> an event or something) so that object A can in turn call a method in
object
> C?
>
> I've learned that object B can call a static method in object A. But the
> rule for static methods is that they can only operate on the class to
which
> they belong (meaning that a static method in object A could not call a
method
> in object C).
>
> You see, objects "B" and "C" do specialized work as all good objects
should
> do. But I need object "A" to be able to coordintate the activities
between
> "B" and "C".
>
> How do I handle this?
Re: A creates-->B,C. B call-> A call-> C?
The best approach to this kind of problems is use of interfaces. If you remember
the GUI event hadling you can approach your problem in the same manner.
In class B have a method something like addXXXListener() and in B store all
the handlers. When you are done with B invoke method on all handlers. I
am giving a sample skelton.
public interface MyOwnListener
{
public void inovkeme();
}
public class B
{
ArrayList listeners = new ArrayList(5);
public void addMyOwnListener(MyOwnListener l)
{
if(l != null)
listeners.add(l);
}
public void removeListener(MyOwnListener l)
{
if(l != null)
listeners.remove(l);
}
// Whe you are done with B call invokeme on all listeners.
public void done()
{
for(final Iterator ite=listeners.iterator(); ite.hasNext();
{
MyOwnListener l = (MyOwnListener)ite.next();
l.invokeme();
}
}
}
public class A implements MyOwnListener
{
public A()
{
B b = new B();
b.addMyOwnListener(this);
}
//Invoke when B is done.
public void invokeme()
{
C c = new C();
}
}
"rneal" <rneal@imseries.com> wrote:
>
>I hope I am missing something obvious here:
>
>Say I have three objects A, B, and C. My program starts with object "A".
> I use object "A" to create instances of objects "B" and "C". Object "B"
>does some work and when "B" completes its work, I want it to raise an event
>that object "A" can see so that object "A" can respond by making object
"C"
>do some work.
>
>How can I enable object B to effectively call a method in object A (by raising
>an event or something) so that object A can in turn call a method in object
>C?
>
>I've learned that object B can call a static method in object A. But the
>rule for static methods is that they can only operate on the class to which
>they belong (meaning that a static method in object A could not call a method
>in object C).
>
>You see, objects "B" and "C" do specialized work as all good objects should
>do. But I need object "A" to be able to coordintate the activities between
>"B" and "C".
>
>How do I handle this?