java - Best practices in achieving a great cohesion from multiple interfaces -
first, sorry imperfection in wording, let me know if there unclear points.
while building class structure of java application, wonder if there known best practices i've not found yet in case.
for example, there a, b, c , more classes specifying device types, , each device has tokenizer, parser, , compiler.
first, have 4 interfaces these.
interface device { public void x(); public int y(); public string z(); ... } interface tokenizer {...} interface parser {...} interface compiler {...} and, actual concrete classes are,
public class adevice implements device {...} public class bdevice implements device {...} public class cdevice implements device {...} class atokenizer implements tokeninzer {...} class btokenizer implements tokeninzer {...} class ctokenizer implements tokeninzer {...} class aparser implements parser {...} ... class acompiler implements compiler {...} ... additionally , importantly, "device" classes , interface public contained in stub lib. else included in actual library , not visible users generate scripts referring api.
here question, (long introduction :[ )
how achieve cohesion between each type , corresponding interfaces.
in more detail, there way determine whether paired right, between each type of device interface , corresponding tokenizer, parser, or compiler interface in compile time?
for example, there manager class uses "device" objects , "tokenizer", "parser", , "compiler" objects. can see, "adevice" objects can pair "atokenizer", "aparser", or "acompiler". however, if define type of device objects interface, there no way can tell if objects corresponding right "tokenizer", "parser", or "compiler" object, except "instanceof" operator in run time. instance, "adevice" can matched "btokenizer", "cparser", , forth, , not generate compile error.
i hope hear answer or redirect references.
one solution paramaterize interfaces:
interface tokenizer<d extends device> { ... } interface parser<d extends device> { ... } interface compiler<d extends device> { ... } then implementations be:
class atokenizer implements tokeniser<adevice> { ... } class btokenizer implements tokenizer<bdevice> { ... } class ctokenizer implements tokenizer<cdevice> { ... } class aparser implements parser<adevice> { ... } . . . class acompiler implements compiler<adevice> { ... } you have method in manager class restrict them same type:
public <d extends device> void dostuff(tokeizer<d> tokenizer, parser<d> parser, compiler<d> compiler) { ... } the compiler allow used descibe:
dostuff(new atokeizer(), new aparser(), new acompiler()); // works dostuff(new atokeizer(), new aparser(), new bcompiler()); // compiler error
Comments
Post a Comment