Very basic / easy approach: Implemenbt a simple subscribe / publish framework.
- Create an interface that defines a call back method.
- Implement the callback interface in Class C.
- provide a means by which class C anc register the callback with each instance of Class B; do so.
- When the timer, in a specific instance of class b, fires, call the callback.
For eaxmple:
public interface BlammyHoot
{
void hoot(); // this is the call back.
}
public class C implements BlammyHoot
{
public void hoot()
{
// implement the callbe method here.
}
}
public class B
{
private List<BlammyHoot> hootList = new LinkedList<BlammyHoot>();
public void registerBlammyHoot(final BlammyHoot theHoot)
{
if (theHoot != null)
{
hootList.add(theHoot);
}
}
public void respondToTimerTimeout()
{
for (BlammyHoot hootElement : hootList)
{
hootElement.hoot();
}
}
}
Caveat: zero testing performed on the obove java code (including, but not limited to, I did not compile it).
1
solved Accesing an Instance of another class [closed]