java - Map from object to a dynamic string -
right have 60 message
types passed getstuff(message)
method of class implements containerofthings
. there multiple variations of containerofthings
such boxofstuff
, bagoftricks
both of realize getstuff(message)
method generates string based on member variables. result may have pre-pended or post-pended data such labels or concatenated data. see code below.
public class boxofstuff implements containerofthings { private string var1; private string var2; private string varn; public string getstuff(message message) { if (message.equals(message.get_stuff1)) return var1; else if (message.equals(message.get_stuff2)) return "var2 is: " + var2; else if (message.equals(message.get_stuffn)) return varn + "\n"; // etc. each message.get_* } // getters , setters each var* } public class message { private string id = null; private message(string id) { this.id = id; } public final string tostring() { return this.id; } public static final message get_stuff1 = new message("v1"); public static final message get_stuff2 = new message("v2"); public static final message get_stuffn = new message("vn"); }
i trying find design meets following objectives. (1) string returned getstuf() needs reflect current state of implementing class's member fields. (2) prefer away incredibly long series of if / else if
blocks. 1 concern ease of potentially changing persistent data-driven configurable object approach map
lends towards. (3) design should allow simple maintenance and/or edits.
one design work little messy create map key/values initialized in constructor , reset key/value pair inside each setter method. in way, response getstuff(message)
updated new content after changes (ie: in setvar*()
method). other thoughts?
i think you'll need 2 maps. 1 map<message, string>
value format string (i.e. passed string.format()
). second map map<message, field>
should self explanatory once take @ reflection libs. these need setup @ init time after getstuff()
method should clean , setters won't affected @ all.
btw, java doesn't prefix interfaces i
.
Comments
Post a Comment