java - Is placing all setters of derived class to util class a good design? -
i read codes this:
public class base { protected map<string, object> attrmap = new hashmap<>(); public map<string, object> getattrmap() { return this.attrmap; } } public class deriveda extends base{} public class derivedb extends base{} public class util { public void setderivedaattrx(base base, object object) { base.getattrmap().put("deriveda_x", object); } public void setderivedaattry(base base, object object) { base.getattrmap().put("deriveda_y", object); } public void setderivedbattrx(base base, object object) { base.getattrmap().put("derivedb_x", object); } public void setderivedattrz(base base, object object) { base.getattrmap().put("derived_z", object); } }
i asked implementor of codes why design this, here answer:
- we can't let setters in
base
, because it's set derived attributes. - if move setters corresponding derived class, it's hard handle
setderivedattrz
.(note can set attribute z bothderiveda
,derivedb
)we may havebase
reference , set attribute z. know it'sderiveda
orderivedb
indeed, don't know it's 1 exactly. can't cast derived class , call derived setters. - since place these setters in
base
or derived class both have shortcomings, comesutil
class handle setters.
so question, design case?
i think bad design. basic of object-oriented programming call methods on objects, , not helper functions objects , method parameters passed.
Comments
Post a Comment