Re: How do you implement global data?
Knute Johnson wrote:
I have some data that I need to have accessible to multiple classes. The
data is initialized from the constructor of one class by reading a
file. That same class has an editor that can modify that data.
Currently I'm just creating the variables as static in the one class.
Findbugs complains that this is "tricky (which it is) and bad practice".
How would you setup shared data under these conditions?
Thanks everybody for their responses. Let me describe one case in a
little more detail and maybe you can give me a better idea about which
way to go.
I have an application. In the main class I create a GUI, the GUI class
loads some initialization data from a file, creates instances of several
other classes that use some of that data and has an editor that modifies
some of that shared data. Originally I put all of the data in a class
of statics along with all of the constant data that was used by any of
the classes. Then I thought maybe it made more sense to put the data in
the class that loaded it or was the primary user. That just made a
mess. The singleton is pretty much like my original idea only it will
be easier to synchronize the data. The data injection idea is good too
and I had thought of that but it only really works one way. Which is
probably OK but then the data would all end up back in my main class.
Another piece of this that is similar, I have an array of locks that I
use to synchronize access to some data files. I don't know how many
locks I need until I load the initialization file in the main class.
These locks are used by two other classes that read and write these
files. I created the lock references as static and share them that way.
So I don't know if that makes it any more clear about where I want to
go. I would appreciate any ideas or comments. I would like to know
what it is about the singleton that anyone thinks is superior to a class