java - How to pass different objects (having same parent) to a constructor and how to identify type of that object in constructor -


i have scenario i'm sending different objects constructor , have identify class of object , set values accordingly.

class abc {     private long id;     private someclass obj;     private string xyzname;     private date date;     private enumtype status;     // , getters , setters }  class extends abc {     private string  someotherid;     private string type;     private string model;     private string manufacturer;     //and props , getters , setters } class b extends abc {     private string someotherid;     private string equipname;     private string model;     private string serialno;     //and props , getters , setters } class c extends abc {     private string someotherid;     private string materialname;     private string desc;     private string serialno;     //and props , getters , setters }  

note: these entity classes

and in controller, i'm doing ops adding, editing, updating(mostly changing statuses) , etc.and every time have enter or log kind of msg history class. like

from acontroller
historyservice.enterlogtohistory(new history(aobject, enumtype.somestatus));

from bcontroller,
historyservice.enterlogtohistory(new history(bobject, enumtype.somestatus));

from ccontroller,
historyservice.enterlogtohistory(new history(cobject,enumtype.somestatus));

class history() {    private long id;    private date date;    private string status;    private string activity; // or msg     // other @transient properties     history(object obj) {     //set above values getting values form obj(using getters)    }    history(object obj, enumtype status) {    this(obj);    // set other @transient properties getting values form obj      // , set value msg;    } 

}

so, problem how identify object whether obj, b, obj or c obj because if know type of object can getters of obj , can set values in history constructor.

so, please me out in this

to rid of instanceof mess, consider using oveloaded constructors:

class history {      history(a a) {         // initialize `a` instance     }      history(b b) {         // initialize `b` instance     }      //... } 

this work if know classes of instances on compile time.


another option switch static factory methods:

public class history {      // private constructor hide instance creation     private history(abc abc) {         this.id = abc.getid();         this.date = new date(abc.getdate().gettime());         // ... common properties     }      public history setstatus(status s) {         this.status = s;         return this;     }      // public static factory methods create instances specified input     public static history of(a a) {         history h = new history(a);         h.type = a.gettype();         // ... properties specific `a`         return h;     }      public static history of(a a, status status) {         return of(a).setstatus(status);     }      public static history of(b b) {         history h = new history(b);         h.model = b.getmodel();         // ... properties specific `b`         return h;     }      // ... } 

then, create history instances, caller invokes:

history ahistory = history.of(a); history bhistory = history.of(b, status.active); 

the main advantage of such approach more stable api introduced, while remains flexible internal refactoring. imagine, decide implement various behavior history of different types, e.g. vehiclehistory , vesselhistory. create classes extending history, override methods in them , refactor few of() methods in history class (so not new history(), new vehiclehistory() called). far never call constructor externally, outer code things remain unchanged -- receives history object before same history.of() method before.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -