class - Sharing Variables in Java Across Classes (Where Persistance and Concurrency Do Not Matter) -
i need share large number of variables amongst many classes. far, i've been passing map parameter between these classes, becoming tedious. other ways can think of doing beans, overkill, because not need persistence, database, overkill same reason, , below 2 ways. (please note concurrency not issue project.) between singleton , interface methods, suggest , why? know of way? imagine there better ways of doing i'm attempting. main requirements reliability , efficiency. in simple tests, both of below methods meet requirements, don't know how might scale. much.
method 1 (singleton):
package development; import java.util.treemap; import java.util.map; public class param { private map<object, object> params = new treemap<object, object>(); private final static param param = new param(); private param() { system.out.println("<instantiated params>"); }; public static param getinstance() { return param; } public void addparam(object k, object v) { params.put(k, v); } public void removeparam(object k) { params.remove(k); } public object getparam(object k) { return params.get(k); } public void printparams() { for(map.entry<object, object> kv : params.entryset()) { system.out.println(kv.getkey() + "->" + kv.getvalue()); } } } package development; public class tester { public void dotest() { param.getinstance().addparam("key 1", "value 1"); param.getinstance().addparam("key 2", "value 2"); param.getinstance().addparam("key 3", "value 3"); new c1().addaparam(); new c2().addaparam(); new c3().addaparam(); param.getinstance().printparams(); } public static void main(string[] args) { new tester().dotest(); } private class c1 { public void addaparam() { param.getinstance().addparam("key 4", "value 4"); } } private class c2 { public void addaparam() { param.getinstance().addparam("key 5", "value 5"); } } private class c3 { public void addaparam() { param.getinstance().addparam("key 6", "value 6"); } } } result: <instantiated params> key 1->value 1 key 2->value 2 key 3->value 3 key 4->value 4 key 5->value 5 key 6->value 6
method 2 (interface):
package development; import java.util.hashmap; import java.util.map; public interface iparam { map<object, object> params = new hashmap<object, object>(); } package development; import java.util.map; public class tester2 { public void dotest() { iparam.params.put("key 1", "value 1"); iparam.params.put("key 2", "value 2"); iparam.params.put("key 3", "value 3"); new c1().addaparam(); new c2().addaparam(); new c3().addaparam(); for(map.entry<object, object> kv : iparam.params.entryset()) { system.out.println(kv.getkey() + "->" + kv.getvalue()); } } public static void main(string[] args) { new tester2().dotest(); } private class c1 { public void addaparam() { iparam.params.put("key 4", "value 4"); } } private class c2 { public void addaparam() { iparam.params.put("key 5", "value 5"); } } private class c3 { public void addaparam() { iparam.params.put("key 6", "value 6"); } } } result: key 1->value 1 key 2->value 2 key 3->value 3 key 4->value 4 key 5->value 5 key 6->value 6
Comments
Post a Comment