Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting a handle on the object which constructed you

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
How do you get a handle of the object which called your constructor?

For example... You have two objects, object1 and object2. object1 constructs a new instance of object2. object2 needs to get a handle on object1 to call one of object1's methods.

I KNOW there HAS to be a better way than passing the object into the constructor... there HAS to be a better (and cleaner) way of doing it. I just don't know what it is :eek:)

Thank you for your time... Liam Morley
lmorley@wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."
 
why does object2 need to call object1 functionality? and if it does, what is object1 creating object2??
it seems like a funny situation to find yourself in. you could make object2 an inner class of object1, that way it would automatically have access to object1's functionality.
 
object1 is a dialog box, object2 is a panel. object2 needs to disable object1's &quot;Apply&quot; button. The &quot;Apply&quot; button can not go in the panel. <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Hi,
hmmm... heh, this is definitely not the better nor cleaner way... ;)
this is not even good coding:

Object1 contains static class variable o1 that is instance of Object1

then you can call from Object1's methods from Object2 using Object1.o1.someMethod();

...but as I said, I dont think this is good way either...

I somebody knows the way to get the instance of the calling class, please tell us, it would be very handly in some situations.

-Vepo
 
This is an example of where you can use a callback reference. For instance, when you construct the panel you pass in a reference to your Dialog which the panel then can callback to:

Panel p = new Panel(this);

Then in Panel you have

public class MyPanel extends Panel {

public MyPanel(MyDialog d) {this.dialog = d;}

public void disableButton(){dialog.disableButton();}
}

Or something like that. This is considered tight coupling and is discouraged in many instances unless you know that you aren't going to need to extend the flexibility of your program significantly.
 
Vepo,<br>sorry, no can do. static class variables aren't a good idea in this situation.. but thanks :eek:)<br><br>meadandale,<br>that's the solution I proposed originally, but I think that, theoretically, there has to be a better way. However, as everything has a deadline, that's basically the solution I'm implementing at the moment. Thanks all for your time... <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
There is a better way but it requires a bit more OOD and the creation of some interfaces, especially of the nature of contexts, for example

public interface ButtonMediator
{
public void disableButton(String s);
public void enableButton(String s);
}

public interface PanelContext
{
public ButtonMediator getButtonMediator();
.. // possibly more context specific methods here
}

public class DefaultPanelContext implements PanelContext
{
protected ButtonMediator mediator;
public DefaultPanelContext(ButtonMediator b)
{
mediator = b;
}
public ButtonMediator getButtonMediator()
{
return mediator;
}
}


public class MyFrame extends Frame implements ButtonMediator
{
// concrete implementations of disable/enable
MyPanel mp = new MyPanel(new DefaultPanelContext(this));
...
}

public class MyPanel extends Panel
{
PanelContext = ctx;
public MyPanel(PanelContext p)
{
ctx = p;
}
// disable a button
ctx.getButtonMediator().enableButton(&quot;OK&quot;);

//etc
}

Unfortunately, there is no way at runtime to figure out who created you (kind of like people, we know who our parents are but our knowledge of the 'creator' is elusive).

Regards,

Charles

 
Although I do appreciate the effort, that's a bit too much on a deadline. Thanks, though... <p>Liam Morley<br><A HREF="mailto:"></A><br>&quot;light the deep, and bring silence to the world.<br>light the world, and bring depth to the silence.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top