Re: How do you implement global data?
- From: "Chris Uppal" <chris.uppal@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 11 Apr 2007 09:51:47 +0100
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.
There's some room for debate on how/whether to use the Singleton pattern, but
this sounds like a reasonable occasion to me -- although it really depends on
factors I don't know about (such as whether this is library code or application
code, and what sort of execution environment it'll run it -- all "global"
concerns, which is reasonable given that your design issue is itself global).
One specific thing that you've mentioned makes me suspicious of the current
design -- if there are several items of closely related data, then you'd
normally expect them to co-exist in some object with some well-defined role(s).
It's not clear that your collection of static variables constitutes a clear
abstraction.
In general, pulling a bunch of related stuff out into its own object can be
expected to suggest further factorings and re-assignments of responsibility
which will simplify the code which uses it.
(For instance, if this data were represented as an object, then your editor
would be a GUI for editing objects of that type, rather than having the
"owning" class of the variables hardwired into it. That would simplify
testing, and all sorts of things. For instance allowing the user to make and
review changes, without immediately installing those changes into the global
state, would be trivial instead of messy.)
The previous three paragraphs apply whatever design you end up with, Singleton,
Dependency Injection (stupid name), or anything else.
-- chris
P.S. If you do decide to use Singleton, don't mess around trying to prevent
people creating other objects of the same class -- that is at best a waste of
time, and may lead to cripplingly inflexibility.
.
- References:
- How do you implement global data?
- From: Knute Johnson
- How do you implement global data?
- Prev by Date: Re: Am I Crazy? V. simple math question.
- Next by Date: How to reach HTTPSession from Spring application (non-MVC)?
- Previous by thread: Re: How do you implement global data?
- Next by thread: Re: How do you implement global data?
- Index(es):
Loading