java - how to choose what type of object to instantiate based on form field? -
java - how to choose what type of object to instantiate based on form field? -
i have struts2 form takes in maintenance object. there different types of maintenance - sake of brevity let's there removepart , installpart. form includes fields both, user sees 1 - based on user's selection in first dropdown.
what right (best?) way determine maintenance class instantiate 1 time action receives data? best i've come far below, though can't help thinking there improve way it.
edit 6/24 14:18 gmt: removedpart , installedpart classes have fields don't correspond each other.
public class maintenance { private string maintenancetype; private string removedserialnumber; private string removedpartname; private string removedpartdescription; private string removedpartposition; private string installedserialnumber; private string installedpartname; private string installedpartsource; // getters , setters } public class removedpart { private string serialnumber; private string partname; private string partdescription; private string partposition; public static createremovedpart(maintenance maintenance) { homecoming new removedpart(maintenance.getremovedserialnumber(), maintenance.getremovedpartname(), maintenance.getremovedpartdescription(), maintenance.getremovedpartposition()); } private removedpart() { this.serialnumber = serialnumber; this.partname = partname; this.partdescription = partdescription; this.partposition = partposition; } // getters , setters } public class installedpart { //similar removedpart } public class maintaction extends actionsupport { maintenance maintenance; public string execute() { if (maintenance.getmaintenancetype().equals("remove")) { removedpart removed = removedpart.createremovedpart(maintenance); } else { // thought } // more logic homecoming success; }
we can't know how much complex or big design, has been shown, if maintenance class declaring duplicate fields (eg. serialnumber both removed , installed) without using both of them @ same time, , hence they're declared filled chosen type of maintenance page... don't need 3 objects, nor duplicate fields:
declare single maintenance class, single fields post different actions, 1 removal, 1 installation.the type lone help determine kind of maintenance handling method run both types. however, improve turn enum:
public enum maintenancetype { installation(1), removal(2); private int type; private maintenancetype(int t) { type = t; } public int gettype() { homecoming type; } } public class maintenance { private maintenancetype type; private string serialnumber; private string partname; // getters , setters public boolean isinstallation(){ homecoming type == maintenancetype.installation; }; public boolean isremoval(){ homecoming type == maintenancetype.removal; }; }
java oop struts2
Comments
Post a Comment