Java software design -
scenario:
consider following abstract class measurementtype
, subtypes acceleration
, density
:
public abstract class measurementtype { protected string name; protected string[] units; public measurementtype() {} public measurementtype(string name) { this.name = name; } protected void setname(string name) { this.name = name; } protected string getname() { return name; } protected void setunits(string[] values) { this.units = values; } } public class acceleration extends measurementtype { private string name; public acceleration () { super(); } public acceleration(string name) { super(); } } public class density extends measurementtype { private string name; public density() { super(); } public density(string name) { super(); } }
question:
what need subtypes since there isn't in classes differentiates them apart enough (at least in implementations) give them right exist own entities, sake of example, lets had own arrays containing units each distinct type, density have kilograms, grams etc.. , acceleration have mph, kmph etc..
finally have run class made up:
public class run { public run() {} public static measurementtype testtype() { acceleration = new acceleration("meters per second"); return (measurementtype) a; } public static void main(string[] args) { measurementtype mt = testtype(); system.out.println(mt.getclass().tostring()); system.out.println(mt.getclass().getname()); string type = "acceleration"; string[] units = new string[8]; object mtype; if(type.equals("acceleration")) { mtype = new acceleration(); } else if(type.equals("density")) { mtype = new density(); } else { mtype = new object(); } system.out.println(mtype.getname()); } }
this encountered problem thinking about. using strings determine type instantiate, problem is, default right @ end of conditional statements instantiate object because otherwise compiler complain mtype
variable wouldn't have been initialized. can't create measurementtype
object since abstract class.
i guess more of design question, there better way of determining class instantiate? please assume string type = "acceleration"
came drop down or somewhere user interface, have hard coded in because isn't necessary question.
could shed light on design fault see please.
something factory-like codes may help. saves 100 if equals , new xxxx()
statements if have 100 subtypes.
public abstract class measurementtype { protected string name; private static final map<string, class<? extends measurementtype>> typemap = new hashmap<>(); static{ typemap.put("accelaration", acceleration.class); typemap.put("density", density.class); //put here if have others } public static measurementtype getmeasurement(string keystr) throws runtimeexception { if (typemap.containskey(keystr)) { try { return typemap.get(keystr).newinstance(); } catch (exception e) { e.printstacktrace(); throw new runtimeexception("creation failed."); } } //this speical exception type throw new runtimeexception("creation failed: unknown type"); } }
in run
class/method:
string s = "accelaration"; string s2 = "trash"; measurementtype t = null; try { t =measurementtype.getmeasurement(s); t = measurementtype.getmeasurement(s2); //throw ex }catch (runtimeexception e){ //do }
Comments
Post a Comment