![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Sharing objects?
I want to share a single instance of a Data object between two other objects, GraphPanel and ControlPanel. How can I do this neatly? Where do I store the object and how do the other objects reference it?
I could have the two classes extend a class which stores the data object, but I am looking for a more general approach. I thought of using an interface but I don't think that would work. Any ideas? Thanks. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
You could always pass each of the classes a reference to the Data object
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
That's true, but what if I need to set the data object to a new object? Then the other classes will have references to the old objects.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
union
__________________
|
|
|
|
|
|
#5 | |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Quote:
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
I'm looking for something a little neater, along the lines of accessing the data through a method... data().size(); for example.
|
|
|
|
|
|
#7 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 750
Rep Power: 3
![]() |
Unless you're going to keep multiple of the shared objects around, why not just change the data of the original object? Does that work for your project?
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
I want to be able to change the type of object, e.g., Data data = new TypeOfData;
I am still looking for a solution. The only way I can think of is to make the objects that use the data field subclasses of a new class. |
|
|
|
|
|
#9 |
|
Professional Programmer
|
If you want the ability to change data types, make Data an Abstract class, and declare all other types of data from it.
Data d = new FancyDataType(); etc.. |
|
|
|
|
|
#10 |
|
Expert Programmer
|
The problem is storing a reference to the object; for example, if I have 2+ classes with a pointer to Data d and in one class I call d = new FancyDataType(), all the other classes will still reference the old data type. I'm trying to find the best way to make all the classes see the same variable.
Thanks for the help so far. If I figure something out I'll post it. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|