class - Java, differing object fields in child classes and how to work with them in the least painful way -
(edited down big wall o' text better sum up) problem thus:
i have collection of abstractmyclass. there number of concrete child classes in collection. each child class contain field particular type of object. there dozen different children of abstractmyclass, each own object field. each of these objects has no shared parent class outside of object.
for example, myclassa may have string, myclassb may have integer, myclassc may have mycustomclass, etc. different objects unfortunate necessity in way done.
the thing is, collection needs evaluated and, given right set of conditions, object(s) within abstractmyclass must extracted, examined, manipulated, stored, set aside, etc later operations. there variety of potential operations based on conditions myclass/object type within, operations such handling data within myclass may not viable other, more centralized classes (ie, class managing thread pool) may need deal them. leaves me need handle disparate object types. can done, cannot think of reasonably clean or dynamic way handle it. sure, try following:
- use generics, can eliminate child classes, outside of class, don't know object t or how handle without more muckery.
- typecheck everything, makes rather lengthy , ugly conditional, , must maintained if new object types introduced.
- delegates within abstractmyclass, means more classes build handle each instance, , delegates may not able handle of necessary functions.
- a wrapper object field every object type. yay, let's nullcheck everything.
you can see predicament. there "good" way handle sort of thing, or 1 of issues java can't directly handle may not have enough info @ runtime , works around in varying ways?
it's hard answer without concrete example without knowing must response, there 2 clean, oo solutions see.
first solution: old polymorphism:
public void handleresponse(abstractresponse response) { response.handle(); }
in short, response responsible handling when has been received. response knows own type, , has access own data.
second solution: visitor pattern, allows externalizing response handling without doing instanceof checks , casts know type of response:
public interface responsevisitor { void visita(responsea responsea); void visitb(responseb responseb); } public abstract class abstractresponse { public abstract void accept(responsevisitor visitor); ... } public class responsea extends abstractresponse { @override public void accept(responsevisitor visitor) { visitor.visita(this); } } public class responseb extends abstractresponse { @override public void accept(responsevisitor visitor) { visitor.visitb(this); } } public class theresponsevisitorimplementation implements responsevisitor { @override public void visita(responsea responsea) { ... } @override public void visitb(responseb responseb) { ... } } ... public void handleresponse(abstractresponse response) { responsevisitor visitor = new theresponsevisitorimplementation(); response.accept(visitor); }
Comments
Post a Comment