i trying use java generics particular use case. there generic class node
, there lot of implementing class nodeboolean
in example.
public abstract class node<t> { t defaultvalue; public node(t defaultvalue) { this.defaultvalue = defaultvalue; } protected abstract t parse(string input) ; public t getvalue() { try { // here logic string value db or other source string input = "true"; return parse(input); } catch (exception e) { return defaultvalue; } } }
implementing class
class nodeboolean extends node<boolean> { public nodeboolean(boolean defaultvalue) { super(defaultvalue); } @override protected boolean parse(string input) { return boolean.parseboolean(input); } }
test class
class testnode { public static void main(string[] args) { nodeboolean node = new nodeboolean(true); system.out.println(node.getvalue()); } }
problems facing approach
suppose there multiple variants of constructor of class
node
implementing class need define them visibility. problem can solved using builder pattern wondering other possible solutions.there lot of implementing class having 1 override method
parse
. looking way define different parsers based oninstanceof
ofdefaultvalue
in given example in generic classnode
itself. ,node
class notabstract
.i want give functionality define
parse
method takesstring
, based on user requirement returnt
value of while creating object ofnode
class itself. way if have 1 occurrence of specific object type ofnode
can created without defining new implementing class.
i looking solution solves above 3 problems. if not clear @ point let me know.
i think exposing constructors, or using builder or factory patterns possibilities here. but, can mitigated next answer.
instead of having
parse
method abstract, why not pass function node handles parsing?
public class node<t> { private t defaultvalue; private function<string, t> parser; public node(t defaultvalue, function<string, t> parser) { this.defaultvalue = defaultvalue; this.parser = parser; } public t getvalue() { // depending on parser, re-parsing every time called expensive! try { // here logic string value db or other source string input = "true"; return parser.apply(input); } catch (exception e) { return defaultvalue; } } }
you can create node doing this
node<boolean> node = new node<>(s -> boolean.parseboolean(s))
you set default value in parser function, removing need store in node - depends on requirements.
- see 2.
edit i've using java 8's function interface , lambdas here, can in java 1.0 defining own function interface.
public interface function<i, o> { o apply(i); }
and implement in way - concrete class, anonymous class, etc - required parsing need do.
node<boolean> node = new node<>(new function<string, boolean>() { public boolean apply(string s) { return boolean.parseboolean(s); } });
edit 2
in response comment pre-defined types, can take few different approaches.
public class nodefactory { private static final function <string, boolean> boolean_parser = s -> boolean.parseboolean(s); .// plus more integers, double, etc, required private nodefactory() { // no-op } public static node<boolean> booleannode(boolean defaultvalue){ return new node<boolean>(defaultvalue, boolean_parser); } // plus more integer, double, etc, required }
or can take extension route
public class booleannode extends node<boolean> { public booleannode(boolean defaultvalue) { super(defaultvalue, s -> boolean.parseboolean(s)); // or use static parser in factory example } }
in case, keeping node
non-abstract allows lot of flexibility users, use case may not require that.
Comments
Post a Comment